Questions tagged [templates]

The templates tag is used in multiple contexts: generic programming (especially C++), and data/document generation using template engines.

1353
votes
13answers
351k views

Why can templates only be implemented in the header file?

Quote from The C++ standard library: a tutorial and handbook:The only portable way of using templates at the moment is to implement them in header files by using inline functions.Why is this?(...
876
votes
5answers
113k views

Where and why do I have to put the “template” and “typename” keywords?

In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway? I have the following code:template <typename T, typename Tail> //...
390
votes
23answers
107k views

Is it possible to write a template to check for a function's existence?

Is it possible to write a template that changes behavior depending on if a certain member function is defined on a class?Here's a simple example of what I would want to write:template<class T&...
139
votes
3answers
18k views

Why do I have to access template base class members through the this pointer?

If the classes below were not templates I could simply have x in the derived class. However, with the code below, I have to use this->x. Why?template <typename T>class base {protected:...
330
votes
7answers
32k views

Pretty-print C++ STL containers

Please take note of the updates at the end of this post.Update: I have created a public project on GitHub for this library!I would like to have a single template that once and for all takes care ...
205
votes
30answers
155k views

How can I add reflection to a C++ application?

I'd like to be able to introspect a C++ class for its name, contents (i.e. members and their types) etc. I'm talking native C++ here, not managed C++, which has reflection. I realise C++ supplies some ...
295
votes
6answers
75k views

What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

Can some one please describe the usage of the following characters which is used in ERB file:<% %><%=%><% -%><%# %>what's the usage of each one ?
370
votes
10answers
234k views

Storing C++ template function definitions in a .CPP file

I have some template code that I would prefer to have stored in a CPP file instead of inline in the header. I know this can be done as long as you know which template types will be used. For example:...
37
votes
4answers
12k views

Why should the implementation and the declaration of a template class be in the same header file? [duplicate]

Why should the implementation and the declaration of a template class be in the same header file? Could any of you explain it by example?
137
votes
5answers
33k views

What is the curiously recurring template pattern (CRTP)?

Without referring to a book, can anyone please provide a good explanation for CRTP with a code example?
39
votes
5answers
30k views

overloading friend operator<< for template class

I have read couple of the question regarding my problem on stackoverflow now, and none of it seems to solve my problem. Or I maybe have done it wrong...The overloaded << if I make it into an ...
99
votes
13answers
49k views

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 herehttp://www.gotw.ca/gotw/071.htmbut not the same:...
81
votes
3answers
52k views

Why do I get “unresolved external symbol” errors when using templates? [duplicate]

When I write C++ code for a class using templates and split the code between a source (CPP) file and a header (H) file, I get a whole lot of "unresolved external symbol" errors when it comes to ...
44
votes
3answers
34k views

Derived template-class access to base-class member-data

This question is a furtherance of the one asked in this thread.Using the following class definitions:template <class T>class Foo {public:Foo (const foo_arg_t foo_arg) : _foo_arg(...
232
votes
10answers
133k views

Can a C++ class member function template be virtual?

I have heard that C++ class member function templates can't be virtual. Is this true? If they can be virtual, what is an example of a scenario in which one would use such a function?
163
votes
5answers
92k views

Django template how to look up a dictionary value with a variable

mydict={"key1":"value1", "key2":"value2"}The regular way to lookup a dictionary value in a Django template is {{ mydict.key1 }}, {{ mydict.key2 }}. What if the key is a loop variable? ie:{% for ...
42
votes
1answer
3k views

What is a nondeduced context?

I've been exposed to this question recently and the answers can be summed up to "It's a nondeduced context". Specifically, the first one says it's such a thing and then redirects to the standard for ...
204
votes
13answers
43k views

What are the differences between Generics in C# and Java… and Templates in C++? [closed]

I mostly use Java and generics are relatively new. I keep reading that Java made the wrong decision or that .NET has better implementations etc. etc.So, what are the main differences between C++, C#,...
101
votes
11answers
527k views

How can I echo HTML in PHP?

I want to conditionally output HTML to generate a page, so what's the easiest way to echo multiline snippets of HTML in PHP 4+? Would I need to use a template framework like Smarty?echo '<html>...
283
votes
5answers
103k views

JSP tricks to make templating easier?

At work I've been tasked with turning a bunch of HTML files into a simple JSP project. It's really all static, no serverside logic to program. I should mention I'm completely new to Java. JSP files ...
396
votes
22answers
230k views

How to dynamically change header based on AngularJS partial view?

I am using ng-view to include AngularJS partial views, and I want to update the page title and h1 header tags based on the included view. These are out of scope of the partial view controllers though, ...
298
votes
1answer
124k views

C++ template typedef

I have a classtemplate<size_t N, size_t M>class Matrix {// ....};I want to make a typedef which creates a Vector (column vector) which is equivalent to a Matrix with sizes N and 1. ...
51
votes
4answers
6k views

Why can't the template argument be deduced when it is used as template parameter to another template?

What is wrong in this code?#include <map>template<typename T>struct TMap{typedef std::map<T, T> Type;};template<typename T>T test(typename TMap <T>::Type &...
406
votes
7answers
171k views

Explanation of <script type=“text/template”> … </script>

I just stumbled upon something I've never seen before. In the source of Backbone.js's example TODO application (Backbone TODO Example) they had their templates inside a <script type="text/...
105
votes
6answers
70k views

std::enable_if to conditionally compile a member function

I am trying to get a simple example to work to understand how to use std::enable_if. After I read this answer, I thought it shouldn't be too hard to come up with a simple example. I want to use std::...
56
votes
4answers
16k views

Can someone explain this template code that gives me the size of an array? [duplicate]

template<typename T, size_t n>size_t array_size(const T (&)[n]){return n;}The part that I don't get is the parameters for this template function. What happens with the array when I ...
180
votes
9answers
98k views

What are some uses of template template parameters in C++?

I've seen some examples of C++ using template template parameters (that is templates which take templates as parameters) to do policy-based class design. What other uses does this technique have?
321
votes
5answers
87k views

Difference of keywords 'typename' and 'class' in templates?

For templates I have seen both declarations:template < typename T >template < class T >What's the difference? And what exactly do those keywords mean in the following example (taken ...
110
votes
2answers
10k views

How does `void_t` work

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 ...
121
votes
9answers
10k views

How to emulate C array initialization “int arr[]={ e1, e2, e3, … }” behaviour with std::array?

(Note: This question is about not having to specify the number of elements and still allow nested types to be directly initialized.)This question discusses the uses left for a C array like int arr[20]...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,