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
15
votes
1 answer

Is it possible to transform the types in a parameter pack?

Is it possible to transform the types of a parameter pack and pass it on? E.g. given the following: template struct X {}; template struct make_pointer { typedef T* type; }; template struct make_pointer {…
Georg Fritzsche
  • 93,086
  • 26
  • 183
  • 232
15
votes
1 answer

What is the rule that allows `this->` to access members of dependent base classes?

As we know, the code below is ill-formed because the member x is in a dependent base class. However, changing x to this->x on the indicated line would fix the error. template struct B { int x; }; template struct C :…
Brian Bi
  • 91,815
  • 8
  • 136
  • 249
15
votes
5 answers

Django templates: False vs. None

How can I distinguish between None and False in django templates? {% if x %} True {% else %} None and False - how can I split this case? {% endif %}
Evg
  • 2,526
  • 4
  • 36
  • 54
15
votes
2 answers

Why default argument can't be added later in template functions?

C++ standard section 8.3.6.4 says that For non-template functions, default arguments can be added in later declarations of a function in the same scope. [...] But my question is that why it isn't allowed for template functions? What is the…
15
votes
6 answers

Select an integer type based on template integer parameter

I would like to create a class template which takes an unsigned integer parameter and has a member u_ whose type is the smallest unsigned integer type that will hold the integer parameter. So: template class A { ??? u_; }; For A<0>,…
Edgard Lima
  • 367
  • 2
  • 11
15
votes
1 answer

pre-typedef'ing a variadic-function-pointer argument

I have a function foo that takes a variadic function pointer as its argument. I would like to use "using" to define the argument's type prior to the function declaration. template using TFuncType = void(*)(vARGS ...…
xaxazak
  • 574
  • 2
  • 12
15
votes
3 answers

Make *ALL* Wordpress Categories use their Parent Category Template

I want to change the default template hierarchy behavior, and force all subcategory level pages that don't have their own category template file to refer to their parent category template file. In my other post, Richard M. gave an excellent answer…
Matrym
  • 15,021
  • 33
  • 90
  • 137
15
votes
1 answer

How can I expand call to variadic template base classes?

I have a set of non-orthogonal policies, all of them implementing a common named method, the policies add safety checks. I want users to be able to combine the policies to allow more complex validation without creating policies for each combination…
dvicino
  • 1,459
  • 1
  • 10
  • 18
15
votes
3 answers

Execute different functions depending on template parameter disequation

This is definitely a trivial question, but I couldn't figure out how to do this. I have a template function, say template void my_function(). Now, I have two different implementations for my_function, the first should be used if N…
Matteo Monti
  • 6,982
  • 15
  • 52
  • 99
15
votes
3 answers

C++, rvalue references in function parameters

I'm trying to understand rvalue references. I have seen how they are used in constructors, with things like std::move and std::forward, but I still don't understand why this doesn't work: void func(string&& str) { cout << str << endl; } int…
CarlosHD
  • 163
  • 1
  • 6
15
votes
9 answers

C++ Template problem adding two data types

I have a template class with an overloaded + operator. This is working fine when I am adding two ints or two doubles. How do I get it to add and int and a double and return the double? template class TemplateTest { private: T…
Sara
  • 151
  • 1
  • 3
15
votes
3 answers

Template specialization to use default type if class member typedef does not exist

I'm trying to write code that uses a member typedef of a template argument, but want to supply a default type if the template argument does not have that typedef. A simplified example I've tried is this: struct DefaultType { DefaultType() {…
Frank
  • 215
  • 2
  • 5
15
votes
2 answers

How to get SWIG to apply templates when wrapping a template class containing vectors?

I am trying to use SWIG to wrap (in C#) some c++ code that contains a template class that itself wraps a std::vector. I have seen various references on the internet on how to declare the templates for the vector types, but can't get this to work…
SWilliams
  • 671
  • 6
  • 27
15
votes
3 answers

SFINAE away a copy constructor

Under certain conditions, I'd like to SFINAE away the copy constructor and copy assignment operator of a class template. But if I do so, a default copy constructor and a default assignment operator are generated. The SFINAE is done based on tags I…
user1095108
  • 12,675
  • 6
  • 43
  • 96
15
votes
2 answers

Select2 not using my templateResults or templateSelection options

I am trying to use the select2 ajax call with templates. I am getting the ajax in just fine but it is not using my template functions. the ajax data is: [ {"name":"First thing","otherData":"asdfg"}, {"name":"Second…
Bwata
  • 215
  • 1
  • 2
  • 9