template-meta-programming

Best way (Or workaround) to specialize a template alias

爷,独闯天下 提交于 2020-01-14 13:32:12
问题 Im currently implementing a tiny metaprogramming-based compile-time computations library. If have defined a base class for operators, wich has a result typedef (I have decided to use integral wrappers like std::integral_constant as values instead of raw integral values, to provide an uniform interface along the library), and a n-ary operator base class, that checks if the operators has at least one operand: template<typename RESULT> struct operator { using result = RESULT; }; template

Best way (Or workaround) to specialize a template alias

只愿长相守 提交于 2020-01-14 13:32:06
问题 Im currently implementing a tiny metaprogramming-based compile-time computations library. If have defined a base class for operators, wich has a result typedef (I have decided to use integral wrappers like std::integral_constant as values instead of raw integral values, to provide an uniform interface along the library), and a n-ary operator base class, that checks if the operators has at least one operand: template<typename RESULT> struct operator { using result = RESULT; }; template

C++ Concepts - Can I have a constraint requiring a function be present in a class?

北战南征 提交于 2020-01-14 10:42:54
问题 I have a simple code snippet below, which compiles using: g++-9 -std=c++2a -fconcepts This is trying to define a concept that requires the presence of a function. I would expect the output to be "yes" but it's not... Any idea why? Thanks. #include <iostream> template <typename T> concept bool HasFunc1 = requires(T) { { T::func1() } -> int; }; struct Test { int func1() { return 5; } }; int main() { if constexpr (HasFunc1<Test>) std::cout << "yes\n"; } 回答1: You are testing for presence of a

Is it possible to figure out the parameter type and return type of a polymorphic C++ 14 lambda?

我是研究僧i 提交于 2020-01-12 08:08:53
问题 Starting from this question (Is it possible to figure out the parameter type and return type of a lambda?) I used the proposed function_traits a lot. However, with C++14 polymorphic lambdas have arrived and they gave me a hard time. template <typename T> struct function_traits : public function_traits<decltype(&T::operator())> {}; // For generic types, directly use the result of the signature of its 'operator()' template <typename ClassType, typename ReturnType, typename... Args> struct

Integer sequence of chars from user-defined literal taking strings as parameters

梦想的初衷 提交于 2020-01-12 07:59:06
问题 Currently, only doubles can produce a template of chars in a user defined literal: template <char...> double operator "" _x(); // Later 1.3_x; // OK "1.3"_y; // C++14 does not allow a _y user- // defined operator to parse that as a template of chars Is there a clever way to produce a std::integer_sequence of chars using a user defined literal. In other words, what the code of _y(const char*, std::size_t) would be so that I end up with a std::integer_sequence<char, '1', '.', '3'> ? 回答1: At

Invalid explicitly-specified argument for template parameter which is constexpr

社会主义新天地 提交于 2020-01-11 11:32:24
问题 I have a static_loop construct like this template <std::size_t n, typename F> void static_loop(F&& f) { static_assert(n <= 8 && "static loop size should <= 8"); if constexpr (n >= 8) f(std::integral_constant<size_t, n - 8>()); if constexpr (n >= 7) f(std::integral_constant<size_t, n - 7>()); if constexpr (n >= 6) f(std::integral_constant<size_t, n - 6>()); if constexpr (n >= 5) f(std::integral_constant<size_t, n - 5>()); if constexpr (n >= 4) f(std::integral_constant<size_t, n - 4>()); if

Find number of unique values of a parameter pack

给你一囗甜甜゛ 提交于 2020-01-11 09:20:28
问题 Given a parameter pack with variadic arguments, how can one find the number of unique values in the pack. I am looking for something along the lines of no_of_uniques<0,1,2,1,2,2>::value // should return 3 My rudimentary implementation looks something this template <size_t ... all> struct no_of_uniques; // this specialisation exceeds -ftemplate-depth as it has no terminating condition template <size_t one, size_t ... all> struct no_of_uniques<one,all...> { static const size_t value = no_of

Counting With Template Metaprogramming?

梦想的初衷 提交于 2020-01-10 19:13:23
问题 I've been trying to think up a creative solution to this problem (on and off) for some time, but I have not as of yet been able to. I recently considered that it might be solvable with template metaprogramming, though I am not sure due to my relative lack of experience with the technique. Is it possible to use template metaprogramming (or any other mechanism with the C++ language) to count the number of classes which are derived from some base class such that each derived class is given a

Counting With Template Metaprogramming?

让人想犯罪 __ 提交于 2020-01-10 19:11:47
问题 I've been trying to think up a creative solution to this problem (on and off) for some time, but I have not as of yet been able to. I recently considered that it might be solvable with template metaprogramming, though I am not sure due to my relative lack of experience with the technique. Is it possible to use template metaprogramming (or any other mechanism with the C++ language) to count the number of classes which are derived from some base class such that each derived class is given a

Initializing a static char based on template parameter

吃可爱长大的小学妹 提交于 2020-01-07 02:31:28
问题 I want to do something like this : template<typename T> const char * toStr(T num) { thread_local static char rc[someval*sizeof(T)] str = "0x000...\0"; // num of zeros depends on size of T // do something with str return str; } I'm guessing there's some template metaprogramming I'd have to do but I'm not sure where to start. Edit: I found a related question here: How to concatenate a const char* in compile time But I don't want the dependency on boost. 回答1: Not sure to understand what do you