4

Can anyone explain me why in chapter 13 of the third edition of C++ Programming Language, Stroustrup illustrates default parameters for function templates, although they are not supported by C++ (pre C++11)? This is the example given by Stroustrup in section 13.4.1:

Explicitly specifying the comparison for each call is tedious. Fortunately, it is easy to pick a default so that only uncommon comparison criteria have to be explicitly specified. This can be implemented through overloading:

template<class T, class C>
int compare(const String<T>& str1, const String<T>& str2); // compare using C
template<class T>
int compare(const String<T>& str1, const String<T>& str2); // compare using Cmp<T>

Alternatively, we can supply the normal convention as a default template argument:

template <class T, class C = Cmp<T> >
int compare(const String<T>& str1, const String<T>& str2)

and this is the compiler error:

error: default template arguments may not be used in function templates

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Martin
  • 9,089
  • 11
  • 52
  • 87
  • Probably to illustrate what you can't do. You can however do that with a class. – AJG85 Oct 24 '11 at 19:59
  • If you know they are not supported by Standard and still Stroustrup added it in book probably only Stroustrup can tell why. – Alok Save Oct 24 '11 at 19:59
  • @AJG85 No, there he illustrates what you *can* do, not what you cannot do. – Martin Oct 24 '11 at 20:01
  • 1
    Though Stroustrup is the father of C++ his book is not the standard. The standard was generated after countless hours of arguing and discussion of what was best (at the time). and though default template arguments look very reasonable I am sure that there was some technical reason that made them unfeasible at the time and thus they were explicitly precluded by the standard. I would look at this book as how Stroustrup wanted the language to work (but without seeing all posabilities at the time). – Martin York Oct 24 '11 at 20:10
  • I believe default template parameters interfered with the way the compiler chooses which function to use. – Mooing Duck Oct 24 '11 at 20:22
  • Maybe, @Als, but maybe not. Thus, this question. – Rob Kennedy Oct 24 '11 at 20:30

1 Answers1

9

The author himself explains this on his web site:

Due to an unfortunate oversight, the standard simply bans default arguments for template parameters for a function template. Voted to be corrected in the next standard.

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64