Talk:Template (C++)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
          This article is of interest to the following WikiProjects:
WikiProject Computing (Rated Start-class, Low-importance)
WikiProject icon This article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
Start-Class article Start  This article has been rated as Start-Class on the project's quality scale.
 Low  This article has been rated as Low-importance on the project's importance scale.
WikiProject C/C++ (Rated Start-class, High-importance)
WikiProject icon This article is within the scope of WikiProject C/C++, a collaborative effort to improve the coverage of C/C++ on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
Start-Class article Start  This article has been rated as Start-Class on the quality scale.
 High  This article has been rated as High-importance on the importance scale.
This article falls within the scope of C++.

Removing 'Tips' section[edit]

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

Question about removal[edit]

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 (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? (talk) 18:20, 30 May 2010 (UTC)

Added little list[edit]

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[edit]

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 (talkcontribs) 21:12, 20 March 2008 (UTC)

Partial Specialization of Function Templates[edit]

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 (talk) 15:19, 26 September 2008 (UTC)

Function template edits by anonymous editor[edit]

I am afraid that your contributions may have to be removed, qualifying as original research. I am not unfamiliar with max/min taking arguments of different types, but even the "promote" solution has its problems, the most glaring one being

int j = max(-1, 0u);

which has implementation-defined behaviour, equivalent to:

int j = static_cast<int>(UINT_MAX);

decltype 10:55, 23 January 2009 (UTC)

C++ factorial example incorrect[edit]

// Induction 
template <int N> struct Factorial {
  const int value = N * Factorial<N - 1>::value;
// Base cases via template specialization:
template <>
struct Factorial<0> {
  const int value = 1;
template <>
struct Factorial<1> {
  const int value = 1;

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[edit]

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

They are not keywords. In the example code, Factorial<N - 1>::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<T,U>::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:
template<typename T, typename U> struct promote { typedef T type; };
So promote<double, int>::type would be equivalent to double, for example. decltype (talk) 17:06, 23 January 2010 (UTC)


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[edit]

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 (talkcontribs) 00:55, 6 August 2011 (UTC)

Advantages/disadvantages out of date[edit]

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)

More out-of-date stuff[edit]

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"[edit]

This code is incorrect

   #include <iostream>

   int main()
      // This will call max <int> (by argument deduction)
      std::cout << max(3, 7) << std::endl;
      // This will call max<double> (by argument deduction)
      std::cout << max(3.0, 7.0) << std::endl;
      // This type is ambiguous, so explicitly instantiate max<double>
      std::cout << max<double>(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[edit]

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)