模板和隐式构造函数的类定义之外的友元声明
Friend declaration outside the class definition for template and implcit constructor
我希望我的重载运算符+适用于混合类型。非模板类没有问题。 为了使它与模板类一起使用,我在类中添加了一个朋友运算符+,它可以工作。
template <typename T> class Wrapper
{
T _val;
public:
Wrapper(T val) :_val(val){}
T getValue() const { return _val; }
friend Wrapper<T> operator+(const Wrapper<T>& val1, const Wrapper<T>& val2)
{
return Wrapper<T>(val1.getValue() + val2.getValue());
}
};
int main()
{
Wrapper<int> v1 = 10; // OK, implicit constructor
Wrapper<int> v2(20);
Wrapper<int> result1 = v1 + v2; // OK, both values are of type Wrapper<int>
Wrapper<int> result2 = v1 + 40; // Ok, casting by implicit constructor works
return 0;
}
现在我想像这样将运算符+实现移到类之外:
#include <iostream>
#include <string>
template <typename T> class Wrapper;
template <typename T> Wrapper<T> operator+(const Wrapper<T>& val1, const Wrapper<T>& val2);
template <typename T> class Wrapper
{
T _val;
public:
Wrapper(T val) :_val(val){}
T getValue() const { return _val; }
friend Wrapper<T> operator+ <>(const Wrapper<T>& val1, const Wrapper<T>& val2);
};
// template<class T> Wrapper<T> operator+(const Wrapper<T>&, const Wrapper<T>&)
// template argument deduction/substitution failed
template <typename T> Wrapper<T> operator+(const Wrapper<T>& val1, const Wrapper<T>& val2)
{
return Wrapper<T>(val1.getValue() + val2.getValue());
}
int main()
{
Wrapper<int> v1 = 10; // OK, implicit constructor
Wrapper<int> v2(20);
Wrapper<int> result1 = v1 + v2; // OK, both values are of type Wrapper<int>
// note: mismatched types 'const Wrapper<T>' and 'int'
Wrapper<int> result2 = v1 + 40; // Error
return 0;
}
但它给了我编译错误(将它们粘贴到上面的代码中(。 可以修复它吗?
http://cpp.sh/5j5zg(工作( http://cpp.sh/9saow(不工作(
制作这种不是模板函数的友元函数有点奇怪。但是,您可以通过将friend operator+
设置为模板函数来对其进行一些更改以使其工作:
template <typename T>
class Wrapper
{
T _val;
public:
Wrapper(T val) :_val(val){}
T getValue() const { return _val; }
template <typename U>
friend Wrapper<U> operator+(const Wrapper<U>& val1, const Wrapper<U>& val2);
};
template <typename T>
Wrapper<T> operator+(const Wrapper<T>& val1, const Wrapper<T>& val2)
{
return Wrapper<T>(val1.getValue() + val2.getValue());
}
这几乎可以按您的预期工作,但由于这是一个模板函数,因此隐式转换被禁用。不过,将它们重新添加起来很容易:
template <typename T>
Wrapper<T> operator+(const Wrapper<T>& val1, const T& val2)
{
return Wrapper<T>{ val1.getValue() + val2 };
}
template <typename T>
Wrapper<T> operator+(const T& val1, const Wrapper<T>& val2)
{
return Wrapper<T>{ val1 + val2.getValue() };
}
住在科里鲁
相关文章:
- 在模板类之外定义友元函数的正确方法是什么?
- 2个模板化类的非模板友元函数未定义引用错误
- 未定义的类模板不会实例化以检查友元函数
- 在类内定义的友元函数与类外定义的友元函数的查找规则之间的差异
- 具有定义模板还是非模板的友元函数
- 为什么T是未定义的?我正在尝试实现一个用于双链表的节点类,它不喜欢我使用友元运算符后的T
- 模板和隐式构造函数的类定义之外的友元声明
- 为什么我不能定义一元运算符,然后在 MSVC 的模板类中声明具有相同名称的友元二进制运算符?
- 为什么在定义类之前声明类的对象会在友元类中给出错误,而在友元函数中不会出错
- 正确的友元定义,以授予 std::map 对私有默认构造函数的访问权限
- 如何获取指向模板类中定义的友元函数的函数指针
- 在 c++ 中定义命名空间中模板类的友元函数时出现问题
- 当类和函数具有单独的模板参数时,在类定义之外定义友元函数
- 获取内联定义的友元函数的地址
- 我可以有一个从外部不可见但未在标头中定义的静态友元函数吗?
- 如何正确定义无友元函数
- 如何在局部类中定义友元函数运算符>>?
- 在类中定义友元用户定义的文字运算符
- 在类内定义友元函数模板的情况下,如何避免重定义错误
- c++必须在头文件中定义友元函数