Talk:Template (C++)

Removing 'Tips' section
"Tips for Using Templates" is not encyclopedia-like, anyone object to removing that section? -Wootery 17:01, 21 November 2006 (UTC)

Question about removal
While this article is not particularly well written, the subject of templates is particularly important in many circumstances and should not be removed. —Preceding unsigned comment added by 136.165.3.236 (talk) 15:59, 25 September 2007 (UTC)

However, there exists a more general and complete article, Template_metaprogramming, perhaps this article should be merged to it, or redirected to it? 66.11.179.30 (talk) 18:20, 30 May 2010 (UTC)

Added little list
I think when one lists stuff so he should use list (or tree) ;) Xchmelmilos (talk) 18:47, 11 March 2008 (UTC)

Odd mixing of tenses
I'm not a grammar king but sentences like: "compilers historically generate somewhat esoteric" and "many compilers historically have very poor support" seem to mix tenses incorrectly - the use of the word "historically" especially seems to jar. Either compilers still do xyz or they don't. Based on my experience of current C++ compilers both sentences are either out of date or irrelevant, depending on what exactly they're supposed to be saying. —Preceding unsigned comment added by Jonhanson (talk • contribs) 21:12, 20 March 2008 (UTC)

Partial Specialization of Function Templates
The current C++ standard does not allow partial specialization of function templates. Only class templates can be partially specialized. The article is somewhat misleading in this regard, but technically not incorrect ("such and such is called partial specialization"). —Preceding unsigned comment added by 71.97.78.127 (talk) 15:19, 26 September 2008 (UTC)

Function template edits by anonymous editor
I am afraid that your contributions may have to be removed, qualifying as original research. I am not unfamiliar with  taking arguments of different types, but even the "promote" solution has its problems, the most glaring one being which has implementation-defined behaviour, equivalent to: decltype 10:55, 23 January 2009 (UTC)

C++ factorial example incorrect
I think this example is slightly wrong, "value" should be "static const int value", otherwise it does not compile. Can anybody confirm that, or is it just my compiler? --StCz (talk) 15:20, 1 March 2009 (UTC)
 * You're right. Another possibility is to use unnamed enums, which avoids the problem with static const members and the One Definition Rule (they can only be used where an integral constant expression is required). Anyway, nice find! decltype 18:43, 1 March 2009 (UTC)

Template keywords
What do the keywords  and   mean? --Abdull (talk) 16:35, 23 January 2010 (UTC)


 * They are not keywords. In the example code, Factorial::value refers to the static const data member value of a particular specialization of the Factorial template. Since the definition of the promote template is not shown, it is impossible to tell what promote::type refers to, so this example should not really be there. It's original research, too. However, type would typically be a typedef. For example:  So promote::type</tt> would be equivalent to <tt>double</tt>, for example. <span style="font-family:monospace, monospace;">decltype (talk) 17:06, 23 January 2010 (UTC)

turing
Are Templates Turing complete: in principle would it be possible to perform arbitrary computations during the compiling (rather than just by executing the program afterward)? Alternatively, is the phase of unravelling templates always guaranteed to halt (as it presumably is for macros)? Cesiumfrog (talk) 23:56, 31 January 2011 (UTC)

some overlap with "Generic programming" article
The "generic programming" article has become a long list of language mechanisms for templates/generics-- perhaps it would be better to move such language technical descriptions here. — Preceding unsigned comment added by Brianbjparker (talk • contribs) 00:55, 6 August 2011 (UTC)

Advantages/disadvantages out of date
Several if not all of the disadvantages listed were arguably true 15 years ago, but no longer defensible. (I personally nixed the extensive use of templates in a large project ca. 1995. Today that would be unwise.) The whole section is just a collection of unattributed opinions. Despite the putative "fundamental drawbacks," most of the volume of work done by the standards committees and developers over the last few years has been devoted to templates and their use. The standard template library is now thoroughly incorporated into the language and into recognized best practices. Re-working the section would be a big job, digging up historical documents about problems with templates and how the C++ community addressed them. I say delete the section. Jive Dadson (talk) 01:48, 13 August 2012 (UTC)

Agree. It's ever more out of date now. Deleting. Quimn (talk) 00:10, 3 May 2020 (UTC)

More out-of-date stuff
The reference to Boost enable_if is out of date. It still exists of course, but enable_if has been incorporated into a new and extensive sub-library of the Standard Template Library (STL), along with many other features of Boost. The whole subject of C++ templates is big, and in a state of flux. There are many additions coming with the new C++11 standard, which as of today Aug. 13, 2012, has been published but is not yet not fully implemented in many compilers.

The lead says there are two kinds of templates. C++11 adds another for typedefs. (For technical reasons, the committee selected the keyword "auto.")

I think this article needs to be purged of technical info and opinion, and reduced to links and references. Jive Dadson (talk) 01:58, 13 August 2012 (UTC)

Incorrect code under "Function Templates"
This code is incorrect

#include int main {      // This will call max (by argument deduction) std::cout << max(3, 7) << std::endl; // This will call max (by argument deduction) std::cout << max(3.0, 7.0) << std::endl; // This type is ambiguous, so explicitly instantiate max std::cout << max (3, 7.0) << std::endl; return 0; }

max should be replaced by std::max J.A.Belloc (talk) 13:13, 25 December 2012 (UTC)

Function template error
In the section Function template there's the following text:
 * a type parameter does not need to be a class, it may also be a basic type like int or double.

Which is uncorrect, ‘double’ is not a valid type for a template non-type parameter, the only correct non-type parameters are integrals.--Pau.Montequi (talk) 17:09, 9 December 2013 (UTC)


 * Note that it's talking about type parameters, not non-type parameters. It's saying that the  in   doesn't have to be a class (in an instantiation of the template). It can, of course, be  . Perhaps you could suggest different wording if you think it's unclear. (The wording has been changed a little since your comment, but not in any significant way.) --Zundark (talk) 09:31, 5 July 2018 (UTC)

Pros of templates
So, pros of templates is that templates gives to a C++ programmer a compile-time way of coding without knowing of all classes specification in advance. — Preceding unsigned comment added by RippleSax (talk • contribs) 14:37, 27 November 2015 (UTC)

Portability
A section on portability is to be desired. I saw many years ago advice such as "avoid templates if you ever want to deploy your code to a micro controller." Is this even remotely true today? <em style="text-shadow:1px 1px 0 lightBlue,1px -1px 0 lightBlue,-1px 1px 0 lightBlue,-1px -1px 1px lightBlue">Avindra talk / contribs 20:02, 19 November 2020 (UTC)
 * This article describes some standard features of C++ templates, which are supported by most compilers today. Some compilers (such as g++) have non-standard extensions to the C++ language which are not portable. Jarble (talk) 18:16, 31 August 2021 (UTC)