Questions tagged [templates]

The templates tag is used in multiple contexts: generic programming (especially C++), and data/document generation using template engines. When using this tag on implementation heavy questions - tag the code language the implementation is written in.

The templates tag is used in multiple contexts:

C++ templates

Templates in allow for generic programming and . The C++ Book Guide contains books treating templates as well, especially:

Before asking a question, consider looking into these FAQs first:

There are also useful questions on StackOverflow:

Books

Other templates (PHP, django, drupal, mediawiki, etc.)

There are several varieties of template engines used with web servers, web applications, and web scripting languages. Questions for these types of templates should use the language specific tag.

The web server or scripting language templates are different from templates as used in or generics as used in . These templates are used to help with separating view or presentation of data with the business logic generating or transforming the data.

PHP template questions should use the specific template product tag such as , , etc.

django template questions should use .

drupal template questions should use .

mediawiki template questions should use .

49783 questions
191
votes
5 answers

Default template arguments for function templates

Why are default template arguments only allowed on class templates? Why can't we define a default type in a member function template? For example: struct mycclass { template void mymember(T* vec) { // ... } }; Instead, C++…
Arman
  • 4,387
  • 10
  • 38
  • 63
187
votes
5 answers

How to check if a variable exists in a FreeMarker template?

I have a Freemarker template which contains a bunch of placeholders for which values are supplied when the template is processed. I want to conditionally include part of the template if the userName variable is supplied, something like: [#if_exists…
Dónal
  • 176,670
  • 166
  • 541
  • 787
186
votes
6 answers

Angularjs Template Default Value if Binding Null / Undefined (With Filter)

I have a template binding that displays a model attribute called 'date' which is a date, using Angular's date filter. {{gallery.date | date:'mediumDate'}} So far so good. However at the moment, if there is no value…
Undistraction
  • 38,727
  • 46
  • 165
  • 296
175
votes
14 answers

C++ templates that accept only certain types

In Java you can define generic class that accept only types that extends class of your choice, eg: public class ObservableList { ... } This is done using "extends" keyword. Is there some simple equivalent to this keyword in C++?
mgamer
  • 12,296
  • 23
  • 84
  • 142
172
votes
4 answers

Why should I avoid std::enable_if in function signatures

Scott Meyers posted content and status of his next book EC++11. He wrote that one item in the book could be "Avoid std::enable_if in function signatures". std::enable_if can be used as a function argument, as a return type or as a class template or…
hansmaad
  • 16,551
  • 7
  • 46
  • 89
171
votes
4 answers

Template default arguments

If I am allowed to do the following: template class Foo{ }; Why am I not allowed to do the following in main? Foo me; But I must specify the following: Foo me; C++11 introduced default template arguments and right now they…
user633658
  • 2,005
  • 2
  • 15
  • 16
170
votes
7 answers

std::enable_if to conditionally compile a member function

I am trying to get a simple example to work to understand how to use std::enable_if. After I read this answer, I thought it shouldn't be too hard to come up with a simple example. I want to use std::enable_if to choose between two member-functions…
evnu
  • 5,910
  • 2
  • 23
  • 36
169
votes
3 answers

"Undefined reference to" template class constructor

I have no idea why this is happenning, since I think I have everything properly declared and defined. I have the following program, designed with templates. It's a simple implementation of a queue, with the member functions "add", "substract" and…
Heathcliff
  • 2,578
  • 4
  • 22
  • 36
169
votes
13 answers

How to Debug Variables in Smarty like in PHP var_dump()

I have some variables inside a template and I don't know where I assigned them. I need to know what is inside a particular variable; for instance, say I have a variable in smarty called member. I tried with {debug} but it didn't work, and no popup…
streetparade
  • 29,077
  • 35
  • 97
  • 121
169
votes
7 answers

std::function vs template

Thanks to C++11 we received the std::function family of functor wrappers. Unfortunately, I keep hearing only bad things about these new additions. The most popular is that they are horribly slow. I tested it and they truly suck in comparison with…
Red XIII
  • 5,057
  • 4
  • 22
  • 29
163
votes
3 answers

Static member initialization in a class template

I'd like to do this: template struct S { ... static double something_relevant = 1.5; }; but I can't since something_relevant is not of integral type. It doesn't depend on T, but existing code depends on it being a static member…
Alexandre C.
  • 52,206
  • 8
  • 116
  • 189
161
votes
2 answers

How does `void_t` work

I watched Walter Brown's talk at Cppcon14 about modern template programming (Part I, Part II) where he presented his void_t SFINAE technique. Example: Given a simple variable template that evaluates to void if all template arguments are well…
nonsensation
  • 3,317
  • 5
  • 26
  • 39
160
votes
4 answers

How to create a template function within a class? (C++)

I know it's possible to make a template function: template void DoSomeThing(T x){} and it's possible to make a template class: template class Object { public: int x; }; but is it possible to make a class not within a…
user98188
158
votes
13 answers

What are the differences between "generic" types in C++ and Java?

Java has generics and C++ provides a very strong programming model with templates. So then, what is the difference between C++ and Java generics?
popopome
  • 11,420
  • 14
  • 40
  • 36
157
votes
3 answers

When to use std::forward to forward arguments?

C++0x shows an example of using std::forward: template void foo(T&& arg) { bar(std::forward(arg)); } When is it advantageous to use std::forward, always? Also, it requires to use && in the parameters declaration, is it valid in all…
coyotte508
  • 6,974
  • 5
  • 34
  • 58