问题描述
是否允许在好友声明中为模板参数提供默认值?
Is it allowed to provide a default to a template argument in a friend declaration?
class A {
int value;
public:
template<class T = int> friend void foo();
};
Visual Studio 2015似乎允许它. gcc拒绝了.我在 cppreference 页上找不到任何内容.
Visual Studio 2015 seems to allow it. gcc refuses it. I couldn't find anything on it on the cppreference page.
推荐答案
从C ++ 11开始,该规则在14.1[temp.param]/9
As of C++11, the rule is, specified in 14.1[temp.param]/9
在C ++ 11之前,当然,14.1/9表示默认的模板参数不能在好友模板声明中指定."
Until C++11, of course, 14.1/9 said "A default template-argument shall not be specified in a friend template declaration."
(以上内容几乎逐字复制,由cppreference在默认模板参数,现在也在模板朋友)
(the above is copied pretty much verbatim, by cppreference at Default template parameters, now also mentioned at Template friends)
因此,要使您的程序成为有效的C ++,请在类中定义您的朋友模板,而不仅仅是声明.
So, to make your program valid C++, define your friend template inside the class, don't just declare.
这篇关于具有默认模板参数的朋友功能模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!