Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 具有默认模板参数的友元函数模板_C++_Templates_Friend_Forward Declaration_Default Arguments - Fatal编程技术网

C++ 具有默认模板参数的友元函数模板

C++ 具有默认模板参数的友元函数模板,c++,templates,friend,forward-declaration,default-arguments,C++,Templates,Friend,Forward Declaration,Default Arguments,允许在朋友声明中为模板参数提供默认值吗 class A { int value; public: template<class T = int> friend void foo(); }; A类{ int值; 公众: 模板foo(); }; Visual Studio 2015似乎允许这样做。海湾合作委员会拒绝这样做。我在页面上找不到任何内容。从C++11开始,规则是,在14.1[temp.param]/9 如果友元函数模板声明指定了默认模板参数,则该声明应为定义

允许在朋友声明中为模板参数提供默认值吗

class A {
    int value;
public:
    template<class T = int> friend void foo();
};
A类{
int值;
公众:
模板foo();
};

Visual Studio 2015似乎允许这样做。海湾合作委员会拒绝这样做。我在页面上找不到任何内容。

从C++11开始,规则是,在
14.1[temp.param]/9

如果友元函数模板声明指定了默认模板参数,则该声明应为定义,并应为翻译单元中函数模板的唯一声明

当然,在C++11之前,14.1/9说“在朋友模板声明中不应指定默认模板参数。”

(以上内容几乎是逐字复制的,请参见,现在也在中提到)


<> >,为了使程序有效C++,在类中定义你的朋友模板,不要只是声明。

如果你真的想保持函数脚注()全局,你可以试试这个:

class A
{
    int value;
public:
    template<class T> friend void foo();
};

template<class T = int> void foo()
{
    //you can use private member of A
    A foo;
    auto value = foo.value;
}
A类
{
int值;
公众:
模板foo();
};
模板void foo()
{
//您可以使用
阿福;
自动值=foo.value;
}