template-meta-programming

Struggling with implementation of a type list

和自甴很熟 提交于 2019-12-10 13:49:09
问题 For educational purposes I want to write my own c++11 based typelist. The bare list looks like this: template <typename ... Ts> struct type_list; template <typename T, typename ... Ts> struct type_list<T, Ts ...> { typedef T Head; typedef type_list<Ts ...> Tail; }; template <typename T> struct type_list<T> { typedef T Head; typedef null_type Tail; }; I have created a function called front for extracting the first element: template <typename T> struct front; template <typename TypeList> struct

C++ check whether constructor contains a parameter of given type

ⅰ亾dé卋堺 提交于 2019-12-10 13:32:40
问题 With std::is_constructible one can question some given type for the presence of a certain constructor: struct A {}; struct B { explicit B(int, A, double) {} }; int main() { std::cout<<std::is_constructible<B,int,A,double>::value<<std::endl; //prints true } Suppose one does not know type B . Is there also a way to check whether there exists a constructor in B which contains type A , regardless of the other parameters? (--or, already sufficient, which contains type A in the n-th position?)

Computing the factorial of a small integer at compile time

随声附和 提交于 2019-12-10 13:19:22
问题 I just implemented (once again) a recursive template for computing the factorial of an integer at compile time (who would had thought that some day I'll actually need it!). Still, instead of rolling my own, I went to Boost looking for an answer. However, the factorial function in special math specifically forbids its use with integer types, so I just wrote my own. Still, is there another function in Boost that I should use? Should I cast my integer to double and use the boost::factorial

SFINAE for detecting existence of non-member template function

柔情痞子 提交于 2019-12-10 13:10:38
问题 TL;DR I want to write a template function Process(T value) that behaves differently for different values depending on the existence of a non-member function CreateProcessor<T>() . What can I do for that? I have a problem with SFINAE. Suppose we need to support function CreateProcessor that returns an implementation of interface IProcessor<T> for some type type T . In C++ we can't create several overloads of a function that differ only in return type, so we have to make function

C++ template partial specialization: Why cant I match the last type in variadic-template?

£可爱£侵袭症+ 提交于 2019-12-10 12:45:01
问题 I try to write a IsLast type traits to check if a given type is the last one in a std::tuple , but the code below does not compile. I know how to get around it but I am curious why the compiler does not like it.I guess there must be some rule on specialization of variadic-template that I am not aware of. The code is at: https://godbolt.org/g/nXdodx Error message: error: implicit instantiation of undefined template 'IsLast<std::tuple<std::__cxx11::basic_string<char>, int>, int>' There is also

How to reverse an integer parameter pack?

非 Y 不嫁゛ 提交于 2019-12-10 12:06:19
问题 Sadly, I cannot use any of stl/std libraries from C++, because I am programming for a embedded Operating System which only has available gcc 4.4.4 with bare C++, so, no std::tuple , std::forward , std::apply or std::anything_else . To help understand meta generic generated code, I am presenting a minimal example code compiled with clang because it has a option to show us the generated template-meta-programming/metaprogramming code. This question is just for curiosity because instead of

Check if a given type has a inner template rebind

笑着哭i 提交于 2019-12-10 11:45:28
问题 I need a trait which will check if a given type has an inner template named rebind which takes a single template type parameter. Something like: template <typename X> struct has_rebind { static const bool value = /* ??? */; }; This is similar to what BOOST_TTI_HAS_TEMPLATE does, but I want to implement the trait without the use of Boost TTI, and I do not know how that macro is implemented. 回答1: C++11 and up gives us a wealth of alternatives with which to write arbitrarily complex type traits

Ambiguous metafunction or undefined type

∥☆過路亽.° 提交于 2019-12-10 10:22:25
问题 I am new to metafunctions. I want to write a function that replaces all matches of a certain type in a compound type with some other type. In example: replace<void *, void, int>::type should be int * , replace<void, void, int>::type should be int , etc. I basically failed with two different approaches so far: template < typename C, // Type to be searched typename X, // "Needle" that is searched for typename Y // Replacing type > struct replace { typedef C type; }; // If the type matches the

Getting the Return Type of a Templatized Object's Method

我的未来我决定 提交于 2019-12-10 08:37:23
问题 Say that I have: template <typename T> struct Foo { T& func(); }; And I implement a Foo : Foo<int> bar Now I want to get the return type of bar.func() . I've been trying to force result_of to work with me but to no avail. What I'd really like is to just be able to do result_of_t<foo.func> and be done with it but I imagine it's significantly more difficult? How should I go about getting this return type? EDIT: I was hoping to accomplish this without without respect to how bar was declared.

Hashing types at compile-time in C++17/C++2a

岁酱吖の 提交于 2019-12-10 03:22:24
问题 Consider the following code: #include <iostream> #include <type_traits> template <class T> constexpr std::size_t type_hash(T) noexcept { // Compute a hash for the type // DO SOMETHING SMART HERE } int main(int argc, char* argv[]) { auto x = []{}; auto y = []{}; auto z = x; std::cout << std::is_same_v<decltype(x), decltype(y)> << std::endl; // 0 std::cout << std::is_same_v<decltype(x), decltype(z)> << std::endl; // 1 constexpr std::size_t xhash = type_hash(x); constexpr std::size_t yhash =