强制实例化友元函数
Force instantiation of friend functions
假设我们有一个带友元函数的模板类:
template<class T>
class A {
friend A operator+ (int, const A&);
};
这个函数在下面某处实现:
template<class T>
A<T> operator+ (int i, const A<T>& a) {
...
}
下面还有一个模板类的强制实例化:
template class A<int>;
这是否意味着operator+(int, A<int>)
将被编译?或者我必须单独强制实例化它来实现这一点?
模板参数不会自动转发给friend
声明。您还需要为该函数指定一个模板参数:
template<class T>
class A {
template<class U>
friend A<U> operator+ (int, const A<U>&);
};
实现基本正确,应该是
template<class T>
A<T> operator+ (int i, const A<T>& a) {
// ^^^
// ...
}
相关文章:
- C++模板来检查友元函数的存在
- 如何使用单独文件中的派生类访问友元函数对象
- 模板化的类和友元函数
- 友元函数无法访问私有数据成员 (c++)
- 继承和友元函数,从基类访问受保护的成员
- 如何在友元函数中使用静态成员而不添加前缀 [类名]::
- 在将函数声明为友元时,尖括号的含义是什么?
- 在模板类之外定义友元函数的正确方法是什么?
- 2个模板化类的非模板友元函数未定义引用错误
- C++ 友元函数和私有构造函数
- 使第二个类的构造函数成为第一个类中的友元函数
- 未定义的类模板不会实例化以检查友元函数
- C++类中的友元函数有问题?
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 模板类中的模板友元函数
- C++ 17 个友元函数声明和内联命名空间
- 将派生类的构造函数声明为父类的友元
- 友元函数需要一个帮助程序函数
- 在基/派生类的成员函数/友元函数中派生* 到 Base* 的转换
- 模板类的非模板函数友元