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
16
votes
3 answers

Using enum as template type argument in C++

are there any restrictions / problems using an enum as template (type) argument in C++? Example: enum MyEnum { A, B, C, D, E }; template class MyTemplate { public: _t value; void func(const _t& param) { /* .... */ } }; //…
dyp
  • 38,334
  • 13
  • 112
  • 177
16
votes
4 answers

Why doesn't including headers with templates cause linker errors?

If I include or in multiple translation units (different .cpp files), why doesn't it break the ODR? As far as I know, each .cpp is compiled differently, so std::vector's member functions will be generated for each object file…
barney
  • 2,172
  • 1
  • 16
  • 25
16
votes
3 answers

Is `std::common_type` associative?

The template class std::common_type calculates a common type to a variadic type list. It is defined using the return type of the ternary operator x:y?z recursively. From that definition it is not obvious to me, whether calculating a…
Ralph Tandetzky
  • 22,780
  • 11
  • 73
  • 120
16
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
  • 111,498
  • 10
  • 176
  • 312
16
votes
1 answer

Why can't I have template and default arguments?

I changed a paremeter in a function to accept any kind of object using a template but I can't use it in conjunction with other default parameters, is there something I am missing? #include #include class MyClass { public: …
shuji
  • 7,369
  • 7
  • 34
  • 49
16
votes
2 answers

Check if class is a template specialization?

How can I check if a given type is a specialization of a particular class template? For instance, given template struct A {}; How can I check if CompareT is an A<*> for some type * in the following: template void…
tower120
  • 5,007
  • 6
  • 40
  • 88
16
votes
5 answers

Why do template classes allow member functions which cannot compile?

class P { }; template< typename P > class C : public P { public: void f() { P::f(); } }; int main() { C

c1; return 0; } Just in case my question leaves any room for misunderstanding, here is a code example. If C was not…

qeadz
  • 1,476
  • 1
  • 9
  • 17
16
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
  • 193
  • 1
  • 6
16
votes
2 answers

Can an identity alias template be a forwarding reference?

Consider the following snippet below: template using identity = T; template void foo(identity&&) {} int main() { int i{}; foo(i); } i is an lvalue, hence if foo declares a forwarding reference parameter, it should…
Marc Andreson
  • 3,405
  • 5
  • 35
  • 51
16
votes
3 answers

How to get list items by index in freemarker template?

Is there a way to get list item by index in freemarker template, maybe something like this: <#assign i = 1> ${fields}[i] i'm new to freemarker.
Rasool Ghafari
  • 4,128
  • 7
  • 44
  • 71
16
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
  • 14,119
  • 9
  • 58
  • 116
16
votes
5 answers

Hiding instantiated templates in shared library created with g++

I have a file that contains the following: #include class A {}; void doSomething() { std::map m; } When compiled into a shared library with g++, the library contains dynamic symbols for all the methods of std::map. Since…
jchl
  • 6,332
  • 4
  • 27
  • 51
16
votes
7 answers

C++ Templates: Convincing self against code bloat

I have heard about code bloats in context of C++ templates. I know that is not the case with modern C++ compilers. But, I want to construct an example and convince myself. Lets say we have a class template< typename T, size_t N > class Array { …
Arun
  • 19,750
  • 10
  • 51
  • 60
16
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
  • 225
  • 1
  • 2
  • 10
16
votes
3 answers

Generic way of lazily evaluating (short-circuiting) template conditional types

While playing around with compile-time string (variadic lists of char) manipulation, I needed to implement a way of checking if a compile-time string contained another (smaller) compile-time string. This was my first attempt: template
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
1 2 3
99
100