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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With