Questions tagged [templates]

The templates tag is used in multiple contexts: generic programming (especially C++), and data/document generation using template engines, web template for a pre-designed webpage, or set of HTML webpages. 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 .

54454 questions
180
votes
3 answers

How do we use void_t for SFINAE?

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,627
  • 6
  • 28
  • 41
180
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
  • 18,417
  • 9
  • 53
  • 94
177
votes
8 answers

Officially, what is typename for?

On occasion I've seen some really indecipherable error messages spit out by gcc when using templates... Specifically, I've had problems where seemingly correct declarations were causing very strange compile errors that magically went away by…
dicroce
  • 45,396
  • 28
  • 101
  • 140
175
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
  • 12,250
  • 15
  • 44
  • 36
173
votes
17 answers

Check if a class has a member function of a given signature

I'm asking for a template trick to detect if a class has a specific member function of a given signature. The problem is similar to the one cited here http://www.gotw.ca/gotw/071.htm but not the same: in the item of Sutter's book he answered to the…
ugasoft
  • 3,778
  • 7
  • 27
  • 23
171
votes
10 answers

C++ SFINAE examples?

I want to get into more template meta-programming. I know that SFINAE stands for "substitution failure is not an error." But can someone show me a good use for SFINAE?
rlbond
  • 65,341
  • 56
  • 178
  • 228
170
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
  • 9,175
  • 6
  • 44
  • 63
169
votes
9 answers

Generics/templates in python?

How does python handle generic/template type scenarios? Say I want to create an external file "BinaryTree.py" and have it handle binary trees, but for any data type. So I could pass it the type of a custom object and have a binary tree of that…
keys
  • 1,693
  • 2
  • 11
  • 4
166
votes
5 answers

How to load jinja template directly from filesystem

The jinja API document at pocoo.org states: The simplest way to configure Jinja2 to load templates for your application looks roughly like this: from jinja2 import Environment, PackageLoader env = Environment(loader=PackageLoader('yourapplication',…
Juan Tomas
  • 4,905
  • 3
  • 14
  • 19
160
votes
3 answers

Why use “b < a ? a : b” instead of “a < b ? b : a” to implement max template?

C++ Templates - The Complete Guide, 2nd Edition introduces the max template: template T max (T a, T b) { // if b < a then yield a else yield b return b < a ? a : b; } And it explains using “b < a ? a : b” instead of “a < b ? b :…
Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
158
votes
4 answers

Explicit template instantiation - when is it used?

After few weeks break, I'm trying to expand and extend my knowlege of templates with the book Templates – The Complete Guide by David Vandevoorde and Nicolai M. Josuttis, and what I'm trying to understand at this moment is explicit instantiation of…
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
155
votes
1 answer

Template function inside template class

I have this code: template class MyClass { public: template void foo() { U a; a.invoke(); } }; I want it in this form: template class MyClass { public: template void…
Michael
  • 2,356
  • 3
  • 21
  • 24
154
votes
7 answers

How to access class constants in Twig?

I have a few class constants in my entity class, e.g.: class Entity { const TYPE_PERSON = 0; const TYPE_COMPANY = 1; } In normal PHP I often do if($var == Entity::TYPE_PERSON) and I would like to do this kind of stuff in Twig. Is it…
canni
  • 5,737
  • 9
  • 46
  • 68
149
votes
5 answers

using extern template (C++11) to avoid instantiation

Figure 1: function templates TemplHeader.h template void f(); TemplCpp.cpp template void f(){ //... } //explicit instantation template void f(); Main.cpp #include "TemplHeader.h" extern template void f(); //is…
codekiddy
  • 5,897
  • 9
  • 50
  • 80
149
votes
5 answers

Explicit specialization in non-namespace scope

template class CConstraint { public: CConstraint() { } virtual ~CConstraint() { } template void Verify(int position, int constraints[]) { } template <> void…
Mark
  • 2,181
  • 3
  • 19
  • 28