Questions tagged [template-meta-programming]

Template meta-programming is a meta-programming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled.

Wikipedia entry.

Template meta-programming is a meta-programming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled.

The template outputs include compile time constants, data structures and functions; this technique can be described as a "compile time" execution. It can be used to control the generation of optimized code based on the compile time type being used, and "static polymorphism" (also known by the pattern name CRTP).

2288 questions
622
votes
34 answers

Templated check for the existence of a class member function?

Is it possible to write a template that changes behavior depending on if a certain member function is defined on a class? Here's a simple example of what I would want to write: template std::string optionalToString(T* obj) { if…
andy
  • 18,005
  • 9
  • 30
  • 27
154
votes
24 answers

How can you iterate over the elements of an std::tuple?

How can I iterate over a tuple (using C++11)? I tried the following: for(int i=0; i::value; ++i) std::get(my_tuple).do_sth(); but this doesn't work: Error 1: sorry, unimplemented: cannot expand ‘Listener ...’ into a…
1521237
139
votes
12 answers

C++ templates Turing-complete?

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?
Federico A. Ramponi
  • 46,145
  • 29
  • 109
  • 133
78
votes
1 answer

more spirit madness - parser-types (rules vs int_parser<>) and meta-programming techniques

The question is in bold at the bottom, the problem is also summarized by the distillation code fragment towards the end. I am trying to unify my type system (the type system does to and from from type to string) into a single component(as defined by…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
77
votes
5 answers

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

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…
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
71
votes
1 answer

void_t "can implement concepts"?

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…
Tim Seguine
  • 2,887
  • 25
  • 38
62
votes
5 answers

What is the difference between a trait and a policy?

I have a class whose behavior I am trying to configure. template ServerTraits; Then later on I have my server object itself: template class Server {…}; My question is for my usage above…
59
votes
15 answers

Automatically pick a variable type big enough to hold a specified number

Is there any way in C++ define a type that is big enough to hold at most a specific number, presumably using some clever template code. For example I want to be able to write :- Integer<10000>::type dataItem; And have that type resolve to the…
jcoder
  • 29,554
  • 19
  • 87
  • 130
48
votes
17 answers

Compile-time constant id

Given the following: template class A { public: static const unsigned int ID = ?; }; I want ID to generate a unique compile time ID for every T. I've considered __COUNTER__ and the boost PP library but have been unsuccessful so far.…
David
  • 27,652
  • 18
  • 89
  • 138
47
votes
3 answers

How can I get the depth of a multidimensional std::vector at compile time?

I have a function that takes a multidimensional std::vector and requires the depth (or the number of dimensions) to be passed in as a template parameter. Instead of hardcoding this value I would like to write a constexpr function that will take the…
tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
44
votes
1 answer

Why do C++ templates use the angle bracket syntax?

The titular question refers to the design decisions in the C++ standard that introduced templates around 1990. Why did the designers use <> (angle brackets) instead of, say, () (round brackets)? Doing so would have saved lots of programmers from…
shuhalo
  • 5,732
  • 12
  • 43
  • 60
44
votes
5 answers

Generating one class member per variadic template argument

I have a template class where each template argument stands for one type of value the internal computation can handle. Templates (instead of function overloading) are needed because the values are passed as boost::any and their types are not clear…
user1101674
  • 1,341
  • 2
  • 12
  • 15
40
votes
2 answers

Overloading multiple function objects by reference

In C++17, it is trivial to implement an overload(fs...) function that, given any number of arguments fs... satisfying FunctionObject, returns a new function object that behaves like an overload of fs.... Example: template struct…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
39
votes
4 answers

Recursion using template meta programming

Can somebody explain to me, why the first call using the template function is falling into an infinite loop, while the second compile-time function runs correctly? #include using namespace std; template struct commondivs {…
Exxul
  • 508
  • 5
  • 10
39
votes
2 answers

Default template parameter & lambda in unevaluated context: bug or feature?

We consider the goal of creating two different types, using the exact same syntax. This can be easily done with lambdas: auto x = []{}; auto y = []{}; static_assert(!std::is_same_v); But instead of using lambdas, we are…
Vincent
  • 57,703
  • 61
  • 205
  • 388
1
2 3
99 100