带有返回类型推导的C++11运算符重载
C++ 11 operator overload with return type deduction
我有一个依赖于一种类型的模板类(例如template <typename T> class Vector
)。现在,我想重载算术运算符,这样:
- 我可以将它们与用两种不同类型实例化的Vectors一起使用
- 以与模板实例类型相同的方式推导结果
示例:
Vector<float> fv = {1.5, 2.5};
Vector<int> iv = {1,2};
auto s1 = fv + iv; // s1 MUST be of type Vector<float> == {2.5, 4.5}
auto s2 = iv + fv; // s2 MUST be of type Vector<float> == {2.5, 4.5}
我认为,在一个实现矩阵、向量、多项式等的通用数学库中,这将是一个在可用性方面有所不同的功能。
我用C++11 找到了三种方法来获得这个结果
->
表示法template <typename T, typename U> auto operator +(const Vector<T> &lhs, const Vector<U> &rhs) -> Vector<decltype(lhs[0]+rhs[0])> { Vector<decltype(lhs[0]+rhs[0])> res(lhs); res += rhs; return res; }
declval
template <typename T, typename U> Vector<decltype(std::declval<T>()+std::declval<U>())> operator +(const Vector<T> &lhs, const Vector<U> &rhs) { Vector<decltype(lhs[0]+rhs[0])> res(lhs); res += rhs; return res; }
declval
作为默认类型argtemplate <typename T, typename U, typename R = decltype(std::declval<T>()+std::declval<U>())> Vector<R> operator +(const Vector<T> &lhs, const Vector<U> &rhs) { Vector<R> res(lhs); res += rhs; return res; }
在您看来,实现这样一个功能的最佳方法是什么?如果有第四个更好的解决方案,我将不胜感激。
你认为这样的"互通"运营商是一个有价值的功能吗?
感谢并致以最良好的问候,Davide
这样的运算符是否"好主意"取决于您的用例。但如果你想实现它们,我建议你使用一个源自std::common_type
的traits类来获得更多的控制:
template<typename T, typename U>
struct VectorCommonType : std::common_type<T,U> {};
template<typename T, typename U>
using VectorCommonTypeT = Vector<typename VectorCommonType<T,U>::type>;
这样,您(或类的用户)就可以在需要时对其进行专门化。这样使用:
template<typename T, typename U>
VectorCommonTypeT<T,U> operator+(const Vector<T> &lhs, const Vector<U> &rhs)
{
VectorCommonTypeT<T,U> res(lhs);
res += rhs;
return res;
}
相关文章:
- 为什么 C++ 11 在 ios 类中添加了运算符 bool
- C++11+=当RHS有多个字符串或字符时,运算符不起作用
- 为什么在C++(按位运算符)中(~10)等于-11
- C++11 无符号字符在使用运算符 = 时变为 int
- C++11 类中放置的枚举类的重载运算符
- =删除用户定义的成员功能,除了构造函数,分配运算符C 11
- 运算符的两个不同结果 - 在 c++11 中
- C++11 何时相对于运算符优先级进行算术类型转换
- 字符串流<<运算符不适用于具有 C++11 的宏
- C++11(排序)中的逗号运算符
- 在C++11中查找运算符的规则
- 检查C 11中的运算符的最佳方法
- 返回语句中的C++11显式转换运算符/构造函数
- 是否可以从 C++11 智能指针继承并覆盖相关运算符?
- 在C++11 std::atomic中,++、--和==等运算符使用的memory_order是什么
- C++11移动构造函数和赋值运算符
- C++11与C++98转换运算符的行为发生了变化
- C++11 中的高效算术运算符重载
- 带有返回类型推导的C++11运算符重载
- C++11 运算符" ",带双参数