template-meta-programming

Calling a stateless lambda without an instance (only type)

房东的猫 提交于 2021-02-07 08:01:49
问题 I'm trying to write a wrapper for a "register callback" type of interface from a C library. The issue is quite complicated by the fact that, the library lets you register "variadic" functions by accepting a list of parameter definitions. Then at callback time, the function is expected to extract its arguments from a type-erased list of arguments. Good old C... The interface I'm trying to create is to accept any function, even a lambda, and automatically generate all the machinery to correctly

Calling a stateless lambda without an instance (only type)

☆樱花仙子☆ 提交于 2021-02-07 08:01:36
问题 I'm trying to write a wrapper for a "register callback" type of interface from a C library. The issue is quite complicated by the fact that, the library lets you register "variadic" functions by accepting a list of parameter definitions. Then at callback time, the function is expected to extract its arguments from a type-erased list of arguments. Good old C... The interface I'm trying to create is to accept any function, even a lambda, and automatically generate all the machinery to correctly

N-dimensionally nested metaloops with templates

丶灬走出姿态 提交于 2021-02-07 05:51:07
问题 I am trying to do N-dimensionally nested metaloops with template metaprogramming. The nesting part is trivial, however passing all the arbitrary number of iteration indices as template parameters to the most-inner loop seems problematic. A simple unnested metaloop looks like: template <size_t I, size_t N> struct meta_for { template <typename Lambda> inline meta_for(Lambda &&iteration) { iteration(I); meta_for<I+1, N> next(static_cast<Lambda&&>(iteration)); } }; template <size_t N> struct meta

Ambiguous template with SFINAE dummy parameter

夙愿已清 提交于 2021-02-07 02:54:49
问题 Consider a case where one needs to verify a type T with another template g (could be some enable_if expression, for example) inside a dummy parameter of another template, like this: template<class> struct g { typedef void type; }; template<class, class> struct f {}; template<class T> struct f<T, void> {}; // Case A template<class T> struct f<T*, typename g<T>::type> {}; // Case B int main() { f<int*, void> test; } Here, for the sake of simplicity g doesn't really do anything. The second

Ambiguous template with SFINAE dummy parameter

梦想与她 提交于 2021-02-07 02:54:49
问题 Consider a case where one needs to verify a type T with another template g (could be some enable_if expression, for example) inside a dummy parameter of another template, like this: template<class> struct g { typedef void type; }; template<class, class> struct f {}; template<class T> struct f<T, void> {}; // Case A template<class T> struct f<T*, typename g<T>::type> {}; // Case B int main() { f<int*, void> test; } Here, for the sake of simplicity g doesn't really do anything. The second

Curiously mutually recurring class definitions

梦想的初衷 提交于 2021-02-06 15:01:40
问题 I want type declarations in two classes to mutually depend on each other. Here is a first example that compiles both with clang and gcc: template <class Sum> struct A { using X = char; // (1) using Z = typename Sum::B::Y; // (2) }; template <class Sum> struct B { using Y = typename Sum::A::X; }; struct AplusB { using A = ::A<AplusB>; using B = ::B<AplusB>; }; AplusB::A::Z z; int main() {} There is an interesting moment, however. If you swap lines (1) and (2), then it will fail to compile with

Compile time prime checking

帅比萌擦擦* 提交于 2021-02-06 08:42:13
问题 I need to check is some integer prime in compile time (to put the boolean value as template argument). I've write code that do it well: #include <type_traits> namespace impl { template <int n, long long i> struct PrimeChecker { typedef typename std::conditional< (i * i > n), std::true_type, typename std::conditional< n % i == 0, std::false_type, typename PrimeChecker<n, (i * i > n ) ? -1 : i + 1>::type >::type >::type type; }; template <int n> struct PrimeChecker<n, -1> { typedef void type; }

C++ How to specialize a template using vector<T>?

北战南征 提交于 2021-02-05 10:27:19
问题 Basicly ,I want to make a function behave differently for a vector(type) parameter and a non-vector type parameter . #include <vector> using namespace std; template <typename type> struct is_vector { static const bool value = false; }; template <typename type> struct is_vector<vector<type>> { static const bool value = true; }; template <typename type> type read() { if (is_vector<type>::value) { type vec(10); vec.front()=1;//left of '.front' must have class/struct/union return vec; } else {

C++ How to specialize a template using vector<T>?

孤街醉人 提交于 2021-02-05 10:27:00
问题 Basicly ,I want to make a function behave differently for a vector(type) parameter and a non-vector type parameter . #include <vector> using namespace std; template <typename type> struct is_vector { static const bool value = false; }; template <typename type> struct is_vector<vector<type>> { static const bool value = true; }; template <typename type> type read() { if (is_vector<type>::value) { type vec(10); vec.front()=1;//left of '.front' must have class/struct/union return vec; } else {

Extract template template parameter and variadic template parameter from class template

别说谁变了你拦得住时间么 提交于 2021-01-29 18:33:36
问题 Given the following class template: template <template <typename... Args> class Container, typename... Args> struct container_type_holder {}; I would like to extract its template template parameter and its variadic parameter to reuse in another context. Example: using c1 = container_type_holder<std::map, std::string, int>; using c2 = container_type_holder<tt_parameter<c1>, vt_parameter<c1>>; Where tt_parameter<c1> is some magic trick to extract the template template parameter from c1 and vt