为什么Stroustrup的书演示了默认的函数模板参数,这在当时是不允许的?

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

本文关键字:当时 不允许 参数 函数模板 Stroustrup 默认 为什么      更新时间:2023-10-16

谁能解释一下为什么在 c++编程语言第三版的第13章中,Stroustrup说明了函数模板的默认参数,尽管它们不受c++ (c++ 11之前)的支持?这是Stroustrup在13.4.1节给出的例子:

显式指定每个调用的比较是繁琐的。幸运的是,很容易选择一个默认值,这样就只需要显式指定不常见的比较标准。这可以通过重载实现:

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>
或者,我们可以提供常规约定作为默认模板参数:
template <class T, class C = Cmp<T> >
int compare(const String<T>& str1, const String<T>& str2)

,这是编译错误:

错误:默认模板参数不能用于函数模板

作者自己在他的网站上解释了这一点:

由于一个不幸的疏忽,标准直接禁止了函数模板的模板形参的默认实参。投票决定在下一个标准中修正