4

谁能解释我为什么在C++ Programming Language第三版的第 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)

这是编译器错误:

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

4

1 回答 1

9

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

由于一个不幸的疏忽,该标准简单地禁止函数模板的模板参数的默认参数。投票决定在下一个标准中更正。

于 2011-10-24T20:30:51.723 回答