我正在编写一些函数模板来重载*
矩阵类的运算符。我对类型矩阵做了很多工作double
and complex<double>
。是否可以编写一个返回正确类型的模板函数?例如:
template<class T, class U, class V>
matrix<V> operator*(const T a, const matrix<U> A)
{
matrix<V> B(A.size(1),A.size(2));
for(int ii = 0; ii < B.size(1); ii++)
{
for(int jj = 0; jj < B.size(2); jj++)
{
B(ii,jj) = a*A(ii,jj);
}
}
return B;
}
我想要返回类型V
由自然结果决定T*U
。这可能吗?
EDIT:
后续行动question我询问的已收到的答案提供了适用于此处的其他信息。
在 C++11 中,您可以使用替代函数声明语法:
#include <utility> // for declval
template<class T, class U, class V>
auto operator*(const T a, const matrix<U> A)
-> decltype( std::declval<T>() * std::declval<U>() )
{
//...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)