template-meta-programming

C++ templates Turing-complete?

情到浓时终转凉″ 提交于 2019-11-26 14:52:03
I'm told that the template system in C++ is Turing-complete at compile time. This is mentioned in this post and also on wikipedia . Can you provide a nontrivial example of a computation that exploits this property? Is this fact useful in practice? Example #include <iostream> template <int N> struct Factorial { enum { val = Factorial<N-1>::val * N }; }; template<> struct Factorial<0> { enum { val = 1 }; }; int main() { // Note this value is generated at compile time. // Also note that most compilers have a limit on the depth of the recursion available. std::cout << Factorial<4>::val << "\n"; }

Creating an array initializer from a tuple or variadic template parameters

我们两清 提交于 2019-11-26 13:54:16
I want to represent the description of a persistent memory layout (e.g. Flash or EEPROM device) statically embedded in the program code (preferably in the ROM section), from a set of variadic template parameters, where the necessary offsets are automatically calculated at compile time. The goal is to create an appropriate array initializer, that can be iterated at runtime, without the restrictions you'll get with std::get(std::tuple) , which requires compile time indexing. 1st approach I have created a simple data item descriptor class that binds a particular ID (should be provided as an enum

void_t “can implement concepts”?

▼魔方 西西 提交于 2019-11-26 11:48:47
问题 I was watching the second part of Walter Brown\'s CppCon2014 talk on template metaprogramming, during which he discussed the uses of his novel void_t<> construction. During his presentation Peter Sommerlad asked him a question that I didn\'t quite understand. (link goes directly to the question, the code under discussion took place directly before that) Sommerlad asked Walter, would that mean we actually can implement concepts lite right now? to which Walter responded Oh yeah! I\'ve done it .

Metaprograming: Failure of Function Definition Defines a Separate Function

强颜欢笑 提交于 2019-11-26 11:23:42
In this answer I define a template based on the type's is_arithmetic property: template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){ return to_string(t); } template<typename T> enable_if_t<!is_arithmetic<T>::value, string> stringify(T t){ return static_cast<ostringstream&>(ostringstream() << t).str(); } dyp suggests that rather than the is_arithmetic property of the type, that whether to_string is defined for the type be the template selection criteria. This is clearly desirable, but I don't know a way to say: If std::to_string is not defined then use the

How can I check if a type is an instantiation of a given class template? [duplicate]

我是研究僧i 提交于 2019-11-26 09:02:24
问题 This question already has answers here : Doing a static_assert that a template type is another template (3 answers) Closed 6 years ago . Is it possible to check that a type is an instantiation of a particular template? I have a class template where one of the template parameter must be either an instantiation of a particular template, or some other type. For instance, consider this simple definition of a typelist: struct null_type; template <typename Head, typename Tail> struct typelist { //

TMP: how to generalize a Cartesian Product of Vectors?

心不动则不痛 提交于 2019-11-26 07:29:34
问题 There is an excellent C++ solution (actually 2 solutions: a recursive and a non-recursive), to a Cartesian Product of a vector of integer vectors. For purposes of illustration/simplicity, let us just focus on the non-recursive version . My question is, how can one generalize this code with templates to take a std::tuple of homogeneous vectors that looks like this: {{2,5,9},{\"foo\",\"bar\"}} and generate a homogeneous vector of tuple {{2,\"foo\"},{2,\"bar\"},{5,\"foo\"},{5,\"bar\"},{9,\"foo\"

building and accessing a list of types at compile time

旧街凉风 提交于 2019-11-26 06:41:58
问题 I am trying to achieve the following using c++ template metaprogramming. I wish to build up a list of types and then collect these types together and do further compile-time processing on the list. So for example: foo.h: class Foo { ... }; // INSERT ANY CODE HERE bar.h: class Bar { ... }; // INSERT ANY CODE HERE main.h: #include \"foo.h\" #include \"bar.h\" struct list_of_types { typedef /* INSERT ANY CODE HERE */ type; }; I can insert any code into the slots above, so long as list_of_types:

How to make generic computations over heterogeneous argument packs of a variadic template function?

∥☆過路亽.° 提交于 2019-11-26 06:12:38
问题 PREMISE: After playing around with variadic templates a little bit, I realized that achieving anything which goes slightly beyond the trivial meta-programming tasks soon becomes pretty cumbersome. In particular, I found myself wishing for a way to perform generic operations over an argument pack such as iterate , split , loop in a std::for_each -like fashion, and so on. After watching this lecture by Andrei Alexandrescu from C++ and Beyond 2012 on the desirability of static if into C++ (a

Determine if a type is an STL container at compile time

北战南征 提交于 2019-11-26 05:58:21
问题 I would like to write a template that will determine if a type is an stl container at compile time. I\'ve got the following bit of code: struct is_cont{}; struct not_cont{}; template <typename T> struct is_cont { typedef not_cont result_t; }; but I\'m not sure how to create the necessary specializations for std::vector<T,Alloc>, deque<T,Alloc>, set<T,Alloc,Comp> etc... 回答1: First, you define your primary template, which will have a member which is false in the default case: template <typename

Checking a member exists, possibly in a base class, C++11 version

别说谁变了你拦得住时间么 提交于 2019-11-26 05:23:08
问题 In https://stackoverflow.com/a/1967183/134841, a solution is provided for statically checking whether a member exists, possibly in a subclass of a type: template <typename Type> class has_resize_method { class yes { char m;}; class no { yes m[2];}; struct BaseMixin { void resize(int){} }; struct Base : public Type, public BaseMixin {}; template <typename T, T t> class Helper{}; template <typename U> static no deduce(U*, Helper<void (BaseMixin::*)(), &U::foo>* = 0); static yes deduce(...);