c++ - 带有默认模板参数的友元函数模板

标签 c++ templates friend forward-declaration default-arguments

是否允许在友元声明中为模板参数提供默认值?

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/

相关文章:

c++ - 在 netbeans 中包含 hdf5 的库

C++ 访问 friend 类->成员->公共(public)方法?

c++ - 使用工厂 friend 类来分配新类,好的做法?

c++ - 在运算符重载中使用带有 const 引用的友元函数

html - 尝试使用正则表达式提取一段HTML代码

c++ - 有没有更好的方法将字符串复制到已经包含内容的字符数组

c++ - 创建一个创建全屏覆盖的程序

c++ - 捕获可变参数模板类型?

c++ - 如何使用 int 参数为特定模板部分特化模板函数

c++ - SFINAE 检测类型是否被定义