Why does the Stroustrup book demonstrate default function template arguments that were not allowed at the time?

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

Explicitly specifying a comparison for each call is tedious. Fortunately, it is easy to select a default value, so only unusual comparison criteria should be explicitly specified. This can be implemented by 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 provide a normal convention as an argument to the default template:

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

and this is a compiler error:

error: default template arguments cannot be used in the Templates function

+4
source share
1 answer

The author himself explains this on his website :

Due to poor supervision, the standard simply prohibits default arguments for template parameters for a function template. Voted for the correction in the following standard.

+9
source

All Articles