为什么Stroustrup的书演示了默认的函数模板参数,这在当时是不允许的?
Why does Stroustrup's book demonstrate default function template arguments, which weren't allowed at the time?
谁能解释一下为什么在 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)
,这是编译错误:
错误:默认模板参数不能用于函数模板
作者自己在他的网站上解释了这一点:
由于一个不幸的疏忽,标准直接禁止了函数模板的模板形参的默认实参。投票决定在下一个标准中修正
相关文章:
- 为什么 Clang 不允许"and"作为函数名称?
- 不允许在向量中添加更多元素
- std::带有自定义缓冲区的 iostream 不允许我写入
- Visual Studio 2017 不允许我创建 C++ 专用模板
- 返回时不允许隐式转换
- 为什么 c++(g++) 不允许模板返回类型和函数名称之间有空格?
- 为什么 c++ 不允许(自动)强制转换?
- 为什么 c++11 不允许使用自动
- 为什么不允许成员函数和非成员函数之间的函数重载?
- 为什么不允许使用可变长度数组作为向量元素?
- 余数除法和不允许除以零 (c++) 时遇到问题
- C++从外部类继承的嵌套类;不允许使用不完整的类型
- 在 c++ 中三元运算符中不允许继续(关键字)吗?
- 为什么在指向对象的迭代器上调用函数不允许我更改对象本身?
- 错误:在第 6 行'{'标记之前,此处不允许使用函数定义
- 不允许在类定义之外重写
- 不允许运算符 const 参数调用 const 成员函数
- E0322:不允许使用抽象类类型 " " 的对象
- error dllimport 函数的定义不允许在一个特定的联合中,而其他类、结构和联合将按预期导出
- 为什么Stroustrup的书演示了默认的函数模板参数,这在当时是不允许的?