gpt4 book ai didi

c++ - 基本类型和特定非基本类型的类模板运算符重载

转载 作者:行者123 更新时间:2023-11-30 01:42:44 25 4
gpt4 key购买 nike

我只是在写 MathVector 类

template<typename T> MathVector
{
using value_type = T;

// further implementation
};

但是,该类被认为可以与基本类型一起使用,但也可以与复杂类一起使用

template<typename T> Complex
{
using value_type = T;

// further implementation
};

它提供了例如成员函数

template<typename T> Complex<T>& Complex<T>::operator*=(const Complex<T>& c);
template<typename T> Complex<T>& Complex<T>::operator*=(const T& c);

现在,对于 MathVector 类,还定义了一个乘法:

template<typename T> MathVector<T>& MathVector<T>::operator*=(const MathVector<T>& c);

这对 T=double 没问题, 但对于 T=Complex<double>我希望有可能与 double 相乘无需先将其转换为 Complex<double> (效率更高)。

代码也应该在 CUDA 设备代码中工作这一事实加剧了这一点(为简洁起见,我省略了说明符 __host__ __device__)。这意味着标准库工具不会有帮助。

首先我想到了一个额外的模板参数

template<typename T, typename U> MathVector<T>& MathVector<T>::operator*=(const U& c);

但这对我来说似乎很危险,因为U可以比T多很多或 T::value_type . (事实上​​,我首先在 Complex 类中也有这个参数——编译器无法再决定使用哪个模板,是 Complex 类中的一个还是 MathVector 类中的一个。)

第二种思路是使用模板特化

template<typename T, typename U> MathVector<T>& MathVector<T>::operator*=(const U& c)
{
static_assert(sizeof(T) == 0, "Error...");
}
template<typename T> MathVector<T>& MathVector<T>::operator*=(const typename T::value_type& c)
{
// implementation
}

但这不再适用于基本类型!

我在 C++ Operator Overloading for a Matrix Class with Both Real and Complex Matrices 中看到了这个(或一个非常相似的)问题的解决方案和 Return double or complex from template function , 但它们是使用标准库以 CUDA 无法实现的方式解决的。

所以我的问题是:有没有办法重载处理基本类型和服务于 value_type 的类型的运算符?但不适合其他人 - 不使用 std:: nvcc 的东西编译器会拒绝吗?

最佳答案

您可以制作operator*= 非成员函数模板,并提供所有重载,制作SFINAE生效。

template<typename T>
MathVector<T>& operator*=(MathVector<T>& m, const MathVector<T>& c);
template<typename T>
MathVector<T>& operator*=(MathVector<T>& m, const T& c);
template<typename T>
MathVector<T>& operator*=(MathVector<T>& m, const typename T::value_type& c);

然后称它们为:

MathVector<Complex<double>> m1;
m1 *= MathVector<Complex<double>>{}; // call the 1st one
m1 *= Complex<double>{}; // call the 2nd one
m1 *= 0.0; // call the 3rd one

MathVector<double> m2;
m2 *= MathVector<double>{}; // call the 1st one
m2 *= 0.0; // call the 2nd one

LIVE

关于c++ - 基本类型和特定非基本类型的类模板运算符重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39167802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com