Talk:Template (C++)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

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)[reply]

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 136.165.3.236 (talk) 15:59, 25 September 2007 (UTC)[reply]

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)[reply]

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)[reply]

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)[reply]

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

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)[reply]

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)[reply]

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)[reply]

Template keywords[edit]

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

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)[reply]

turing[edit]

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)[reply]

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)[reply]

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)[reply]

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

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)[reply]

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)[reply]

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)[reply]

Note that it's talking about type parameters, not non-type parameters. It's saying that the T in template<class T> doesn't have to be a class (in an instantiation of the template). It can, of course, be double. 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)[reply]

Pros of templates[edit]

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 (talkcontribs) 14:37, 27 November 2015 (UTC)[reply]

Portability[edit]

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? Avindratalk / contribs 20:02, 19 November 2020 (UTC)[reply]

@Aavindraa: 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)[reply]