是否允许在友元声明中为模板参数提供默认值?
class A {
int value;
public:
template<class T = int> friend void foo();
};
Visual Studio 2015 似乎允许这样做。海湾合作委员会拒绝了。我在 cppreference 上找不到任何内容。页。
最佳答案
从 C++11 开始,规则在 14.1[temp.param]/9
中指定
If a friend function template declaration specifies a default template-argument, that declaration shall be a definition and shall be the only declaration of the function template in the translation unit.
直到 C++11,当然,14.1/9 表示“默认模板参数不得在友元模板声明中指定。”
(上面的内容几乎是逐字复制的,由 Default template parameters 处的 cppreference 逐字复制,现在也在 Template friends 处提到)
因此,为了使您的程序有效 C++,请在类中定义您的友元模板,而不仅仅是声明。
关于c++ - 带有默认模板参数的友元函数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35150772/