gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:20:32 26 4
gpt4 key购买 nike

谁能解释为什么在 C++ Programming Language 第三版的第 13 章中,Stroustrup 说明了函数模板的默认参数,尽管它们不受 C++(C++11 之前)的支持?这是 Stroustrup 在 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)

这是编译器错误:

error: default template arguments may not be used in function templates

最佳答案

作者本人在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.

关于c++ - 为什么 Stroustrup 的书展示了默认的函数模板参数,这在当时是不允许的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7881190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com