template-meta-programming

constexpr version of ::std::function

孤街浪徒 提交于 2020-06-16 02:50:11
问题 I am in search of a ::std::function usable in constexpr. Use case: I have a function which takes a function pointer as an argument, and a second which passes a lambda to the first function. Both are fully executable at compile time, so I want to constexpr them. Eg: template <class _Type> class ConstexprFunctionPtr { private: using Type = typename ::std::decay<_Type>::type; const Type function; public: constexpr inline ConstexprFunctionPtr(const Type f) : function(f) { } template <typename...

Produce std::tuple of same type in compile time given its length by a template argument

扶醉桌前 提交于 2020-05-25 06:19:46
问题 In c++, how can I implement a function with an int template argument indicating the tuple length and produce a std::tuple with that length? E.g. func<2>() returns std::tuple<int, int>(); func<5>() returns std::tuple<int, int, int, int, int>(). 回答1: Here is a recursive solution with alias template and it's implementable in C++11: template <size_t I,typename T> struct tuple_n{ template< typename...Args> using type = typename tuple_n<I-1, T>::template type<T, Args...>; }; template <typename T>

Is there a way to get type of template class from its complete type?

我的梦境 提交于 2020-05-12 04:57:05
问题 I need a meta-function that for given complete class type returns its template (e.g. f<foo<bar>>::type or f<foo<baz>>::type results in foo ). Or it may return true on f<foo<bar>, foo<baz>>::value and false on f<foo<bar>, not_foo<baz>>::value P.S: this was meant to be used with many chrono::duration like classes (but for weight units, mass units and so on). I needed different units not to convert one to another. 回答1: f<foo<bar>>::type or f<foo<baz>>::type results in foo Not exactly (see is-an

Template specialization of variable template and type deduction

随声附和 提交于 2020-04-30 06:24:49
问题 template <class C> C fnc(); template <> int fnc(){return 0;} template <class C> C var; template <> int var = 0; // compile error int main() { } There's a specialization of a fnc function declared without an explicit type indication (such as int fnc<int>() ), so the type of template argument is deduced from the function return type, but that thing does not work for variable templates (it leads to compiler error). Is this a correct behavior or a bug in all compilers a have tested (clang, gcc)?

Template specialization of variable template and type deduction

风流意气都作罢 提交于 2020-04-30 06:24:12
问题 template <class C> C fnc(); template <> int fnc(){return 0;} template <class C> C var; template <> int var = 0; // compile error int main() { } There's a specialization of a fnc function declared without an explicit type indication (such as int fnc<int>() ), so the type of template argument is deduced from the function return type, but that thing does not work for variable templates (it leads to compiler error). Is this a correct behavior or a bug in all compilers a have tested (clang, gcc)?

How do nested templates get resolved in C++?

只愿长相守 提交于 2020-03-23 05:42:22
问题 I recently asked a question about determining whether an iterator points to a complex value at compile time and received an answer that works. The question is here: How can I specialize an algorithm for iterators that point to complex values? And the solution was a set of templates that determine whether one template is a specialization of another: template <class T, template <class...> class Template> struct is_specialization : std::false_type {}; template <template <class...> class Template

How do nested templates get resolved in C++?

一世执手 提交于 2020-03-23 05:42:08
问题 I recently asked a question about determining whether an iterator points to a complex value at compile time and received an answer that works. The question is here: How can I specialize an algorithm for iterators that point to complex values? And the solution was a set of templates that determine whether one template is a specialization of another: template <class T, template <class...> class Template> struct is_specialization : std::false_type {}; template <template <class...> class Template

C++ constexpr : Compute a std array at compile time

对着背影说爱祢 提交于 2020-03-17 08:54:01
问题 I want to convert an "array" of bool to a integer sequence. So I need to compute an std::array at compile time. Here is my code #include <array> template<typename InputIt, typename T > inline constexpr typename std::iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value ) { typename std::iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (*first == value) { ret++; } } return ret; } template<bool ..._values> struct keep

C++ constexpr : Compute a std array at compile time

半城伤御伤魂 提交于 2020-03-17 08:52:04
问题 I want to convert an "array" of bool to a integer sequence. So I need to compute an std::array at compile time. Here is my code #include <array> template<typename InputIt, typename T > inline constexpr typename std::iterator_traits<InputIt>::difference_type count( InputIt first, InputIt last, const T &value ) { typename std::iterator_traits<InputIt>::difference_type ret = 0; for (; first != last; ++first) { if (*first == value) { ret++; } } return ret; } template<bool ..._values> struct keep

Passing different lambdas to function template in c++

泪湿孤枕 提交于 2020-03-01 14:49:47
问题 I have a class Foo that accepts different predicate variants through its constructor. template<typename T> struct Value { T value; }; class Foo { public: template<typename T> Foo(Value<T> &value, function<bool()> predicate) { } template<typename T> Foo(Value<T> &value, function<bool(const Value<T> &)> predicate) : Foo(value, function<bool()>([&value, predicate](){ return predicate(value); })) { } }; This allows me to construct the class with explicit function object: Value<int> i; Foo foo0(i,