Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Stroustrup's book demonstrate default function template arguments, which weren't allowed at the time?

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

like image 586
Martin Avatar asked Jan 18 '23 10:01

Martin


1 Answers

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.

like image 147
Alan Stokes Avatar answered Jan 31 '23 22:01

Alan Stokes