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

Why does the C++ standard specify that unqualified names in a template are non-dependent?

Why is it that the C++ standard specify that unqualified names in a template are non-dependent? e.g. template class Base { public: T x; }; template class C : public Base { public: bool m() { return x == 0; } //…
Danra
  • 9,546
  • 5
  • 59
  • 117
16
votes
1 answer

Overloaded function template disambiguation with `std::enable_if` and non-deduced context

Consider the following code: template struct dependent_type { using type = T; }; template auto foo(T) -> std::enable_if_t{}> { std::cout << "a\n"; } template void foo(typename…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
16
votes
2 answers

Why template argument cannot be deduced in this context?

Could anyone explain why compilers (g++, visual c++) fail to deduce the template argument in this case? struct MyClass { void Foo(int x)& {} void Foo(int x)&& {} }; template void CallFoo(void(T::*func)(int)&) { //create…
John
  • 161
  • 2
16
votes
2 answers

Class A member template function declared as friend in class B can't access private members of class A (Clang only)

Please take a look to this code snippet. I know it does not make much sense, it is just intended to illustrate the problem I am encountering: #include using namespace std; struct tBar { template void…
TheProgammerd
  • 223
  • 1
  • 6
16
votes
2 answers

Limit the number of parameters in a variadic template parameter pack

I have a template function that takes a variable number of arguments. Since you can't force the arguments to be of a certain type I would like at least to force the number of arguments not to be higher that a compile-time determined number(e.g.…
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
16
votes
2 answers

void_t and trailing return type with decltype: are they completely interchangeable?

Consider the following, basic example based on void_t: template> struct S: std::false_type {}; template struct S().foo())>>: std::true_type {}; It can be used as it…
skypjack
  • 49,335
  • 19
  • 95
  • 187
16
votes
4 answers

How to template'ize variable NAMES, not types?

my question is about how to template'ize the name of a class member that should be used. Maybe a simplified & pseudo example: /** Does something with a specified member of every element in a List. */ template // <-- How to define…
Tebas
  • 547
  • 1
  • 6
  • 13
16
votes
8 answers

Dirt-simple PHP templates... can this work without `eval`?

Update- Thanks for all the responses. This Q is getting kind of messy, so I started a sequel if anyone's interested. I was throwing together a quick script for a friend and stumbled across a really simple way of doing templating in PHP. Basically,…
Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
16
votes
2 answers

Shouldn't decltype Trigger Compilation of its Argument?

So I'm perplexed as to how this works. Given: template int foo(T t) { t.foo(); } It seems like this call should fail: decltype(foo(int{ 13 })) fail = 42; cout << fail << endl; Instead it just prints: 42 It works this way on all the…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
16
votes
1 answer

Why is templated functor passed as value and not forwarding reference

In discussion we had here I was playing around with passing functors. C++ STL passes functors as values (seen in std::for_each, std::find_if, std::transform) So declaring mine would be like this. template void call_me(F f) { …
Zereges
  • 5,139
  • 1
  • 25
  • 49
16
votes
1 answer

Initializing a static constexpr data member of the base class by using a static constexpr data member of the derived class

Consider the following code: template struct S { static constexpr int bar = T::foo; }; struct U: S { static constexpr int foo = 42; }; int main() { } GCC v6.1 compiles it, clang 3.8 rejects it with the error: 2 : error: no member…
skypjack
  • 49,335
  • 19
  • 95
  • 187
16
votes
1 answer

Change the Implement Interface template

In Visual Studio 2010, is it possible to change the default template used when implementing an interface? I would like to change the implementation of properties from public int MyProperty { get { throw new…
moi_meme
  • 9,180
  • 4
  • 44
  • 63
16
votes
4 answers

C++ single template specialisation with multiple template parameters

Hallo! I would like to specialise only one of two template types. E.g. template class X should have a special implementation for a single function X::someFunc(). Sample code: main.h: #include…
tauran
  • 7,986
  • 6
  • 41
  • 48
16
votes
5 answers

is_const doesn't work as expected for reference

I wrote a test program: #include #include using namespace std; template void f(T&& t) { cout<()<
vik santata
  • 2,989
  • 8
  • 30
  • 52
16
votes
3 answers

When do we need a .template construct

I made the following program #include #include template struct Class { template void display(){ std::cout<
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
1 2 3
99
100