具有默认模板参数的友元函数模板
friend function template with default template argument
是否允许在友元声明中提供模板参数的默认值?
class A {
int value;
public:
template<class T = int> friend void foo();
};
Visual Studio 2015似乎允许这样做。 海湾合作委员会拒绝了。 我在cpp首选项页面上找不到任何内容。
自 C++11 起,该规则在 14.1[temp.param]/9
中指定
如果友元函数模板声明指定了默认模板参数,则该声明应是一个定义,并且应该是翻译单元中函数模板的唯一声明。
当然,直到 C++11 之前,14.1/9 说"默认模板参数不得在友元模板声明中指定。
(以上内容几乎是逐字复制的,由默认模板参数中的cpp首选项复制,现在在模板朋友中也提到)
因此,为了使您的程序C++有效,请在类中定义您的朋友模板,不要只是声明。
如果你真的想保持你的函数 foo() 全局,你可以试试这个:
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;
}
相关文章:
- C++模板来检查友元函数的存在
- 模板化的类和友元函数
- 在模板类之外定义友元函数的正确方法是什么?
- 2个模板化类的非模板友元函数未定义引用错误
- 未定义的类模板不会实例化以检查友元函数
- 模板类中的模板友元函数
- 具有定义模板还是非模板的友元函数
- 在C++中使用友元函数操作模板类中的私有变量时会发生错误
- 模板类的模板友元函数
- 如何获取指向模板类中定义的友元函数的函数指针
- 如何将模板友元函数声明为可变参数类
- 在 c++ 中定义命名空间中模板类的友元函数时出现问题
- 当类和函数具有单独的模板参数时,在类定义之外定义友元函数
- 如何在模板类中匹配模板友元函数
- 我的运算符重载友元函数无法理解模板类型 T 的问题
- 另一个命名空间和 CRTP 中的模板友元函数
- 在模板类中定义的友元函数
- 具有默认模板参数的友元函数模板
- 在类内定义友元函数模板的情况下,如何避免重定义错误
- 类中定义的友元函数模板是否可用于查找?clang++和g++不一致