【发布时间】:2018-11-27 07:46:26
【问题描述】:
我有一个看起来像这样的模板类:
template <typename T, std::size_t M, std::size_t N> // MxN matrix with elements of type T
struct Mtx{...}
// component wise division
template <typename U> Mtx operator/(const Mtx<U, M, N> &rhs) const
{ return componentDivide(*this, rhs); }
确保operator / 等函数的返回类型“正确”的最佳方法是什么?
例如:
Mtx<float> * Mtx<unsigned> = Mtx<float>
Mtx<float> * Mtx<int> = Mtx<float>
Mtx<float> * Mtx<double> = Mtx<double>
Mtx<double> * Mtx<float> = Mtx<double>
Mtx<short> * Mtx<int> = Mtx<int>
【问题讨论】:
-
用
std::common_type怎么样? -
如果
std::common_type不(或不总是)提供所需的类型,您可以提供自己的专业。 -
这就是我要找的!谢谢!
-
@aCuria:您可以自行回答这个问题,以便对其他人有用。
标签: c++ class c++11 templates return-type