我有一个看起来像这样的模板类:
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>
最佳答案
正如@Someprogrammerdude 在使用 std::common_type
的评论中提到的那样应该为你想要的工作。
#include <iostream>
#include <type_traits>
template <typename T> struct Mtx
{
T _var;
template <typename U>
Mtx<std::common_type_t<T, U>> operator/(const Mtx<U> &rhs) const
{
return this->_var/rhs._var;
}
};
int main()
{
Mtx<float> fObj{ 1.02f };
Mtx<unsigned> uObj{ 1 };
Mtx<int> iObj{ 1 };
Mtx<double> dObj{ 1.02 };
Mtx<short> sObj{ 1 };
std::cout << std::boolalpha
<< std::is_same_v< decltype(fObj / uObj), Mtx<float>> << '\n' // Mtx<float> * Mtx<unsigned> = Mtx<float>
<< std::is_same_v< decltype(fObj / iObj), Mtx<float>> << '\n' // Mtx<float> * Mtx<int> = Mtx<float>
<< std::is_same_v< decltype(fObj / dObj), Mtx<double>> << '\n' // Mtx<float> * Mtx<double> = Mtx<double>
<< std::is_same_v< decltype(dObj / fObj), Mtx<double>> << '\n' // Mtx<double> * Mtx<float> = Mtx<double>
<< std::is_same_v< decltype(sObj / iObj), Mtx<int>> << '\n'; // Mtx<short> * Mtx<int> = Mtx<int>
return 0;
}
输出:
true
true
true
true
true
关于c++ - 选择正确的模板成员函数的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53494940/