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
2 answers

Strong typedefs

Is there any way to make a complete copy of a type so that they can be distinguished in template deduction context? Take the example: #include template struct test { static int c() { static int t = 0; …
Veritas
  • 2,060
  • 2
  • 13
  • 35
15
votes
1 answer

EASTL performance

Today I downloaded and created a sample project of the Electronic Arts STL implementation and the EA's vector is looks much slower for me than the standard. I just created 2 vectors and uploading them with 1 million of items: void…
CsOkemf
  • 175
  • 1
  • 1
  • 7
15
votes
1 answer

Meteor template Blaze how to return only first element of array

For example I have an array: var arr = ['a', 'b'] and want to get only first element in template
Ivan M
  • 529
  • 3
  • 13
15
votes
2 answers

Forwarding initializer list expressions

Initializer list expressions are really convenient for initializing C++ containers: std::vector({1, 2, 3}) ...but it seems that a brace-enclosed initializer list expression, like {1,2,3} will only bind to a function that takes a…
Siler
  • 7,745
  • 5
  • 46
  • 107
15
votes
2 answers

Is the name of a non-static-member dependent when used within a non-static member function?

Both gcc 5.0 and clang 3.6 require the typename keyword in the following example: template struct I { typedef int Type; }; template struct A { int m; void f() { typedef typename I::Type Type; //…
willj
  • 2,951
  • 10
  • 24
15
votes
7 answers

Button template with image and text in wpf

I want to create buttons with images and text inside. For example, i would use different images and text for buttons like 'Browse folders' and 'Import'. One of the options would be to use a template. I had a look at similar question Creating an…
Archie
  • 2,374
  • 8
  • 36
  • 50
15
votes
5 answers

How to avoid "conditional expression is constant" warning with compile-time-constant conditions in template code?

Consider the code: template CByteArray serialize(const T& value) { if (std::is_pod::value) return serializePodType(value); else if (std::is_convertible::value) return serialize(Variant(value)); …
Violet Giraffe
  • 29,070
  • 38
  • 172
  • 299
15
votes
1 answer

Is this code valid? Works with gcc, don't work with clang

The following minimal code compiles on g++, but won't compile on clang++: template T operator*(float a, const T& b) { return b * a; } struct A{ A operator*(float b) const { A a; return a; } }; int main() { …
lvella
  • 10,929
  • 10
  • 42
  • 91
15
votes
3 answers

How can I reduce the compile-time memory footprint of large templates?

Suppose I have a class which contains a large number of other class declarations. Is it possible to spread the cost of these out somehow so that compile-time memory consumption doesn't grow quadratically for nested types? I'm willing to take a hit…
quant
  • 17,534
  • 24
  • 93
  • 186
15
votes
6 answers

C++: How to require that one template type is derived from the other

In a comparison operator: template bool operator==(Manager m1, Manager m2) { return m1.internal_field == m2.internal_field; } Is there any way I could enforce that R1 and R2 must have a supertype or subtype relation?…
Will
  • 5,129
  • 4
  • 19
  • 28
15
votes
1 answer

Android Lightweight HTML Template Engine

I am after a very lightweight template engine that supports / can be embedded inside Android programs. I've looked at the MiniTemplator (I think that is how you spell it) and that looks great but it loads in only from file and I need to load…
Anthoni Gardner
  • 689
  • 1
  • 7
  • 17
15
votes
1 answer

Definition of a class member in the primary template and an implicit instantiation during specialization

I have the following example that I've decomposed from §14.7.3/6 [temp.expl.spec] that defines a class member enumeration in the primary template and subsequently specializes it. The following doesn't compile in clang: template struct A { …
0x499602D2
  • 87,005
  • 36
  • 149
  • 233
15
votes
2 answers

Variadic template argument order, must they always be the right most argument?

I would like to modify an existing class constructor: template< typename T, typename... Ts > MyClass( std::vector& head, Ts& ...tail ); So that a processing flag can be specified: template< typename T, typename... Ts > MyClass( MyEnum myEnum,…
kbirk
  • 3,687
  • 5
  • 38
  • 66
15
votes
3 answers

why no need of forward declaration in static dispatching via templates?

I am playing a bit with static polymorphism, I'm calling a function which internally calls the "right" specialized function depending on the type of the initial argument (basically I'm doing tagging). Here is the code: #include using…
vsoftco
  • 52,188
  • 7
  • 109
  • 221
15
votes
2 answers

Golang templates : how to define array in a variable?

What would be the correct syntax to define an array variable inside a go template ? (here a HTML template). Here's what I tried : {{define "template"}} {{ $x:=[]int{0,1,2} }}{{$x[0]}} {{end}} The error log says : unexpected "[" in…
Nicolas Marshall
  • 2,814
  • 7
  • 26
  • 41
1 2 3
99
100