我为我的应用程序定义了三个类:(int2_整数对)、float2_(浮点数对)和double2_(双精度数),本质上是为了执行复杂的算术运算。
经过以下讨论:
和
我实施了以下解决方案
class float2_;
class double2_;
class int2_ {
    public:
        int x;
        int y;
        __host__ __device__ int2_() : x(), y() {}
        __host__ __device__ inline const int2_& operator=(const int a)          { x = a;            y = 0.;             return *this; }
        __host__ __device__ inline const int2_& operator=(const float a)        { x = (int)a;       y = 0.;             return *this; }
        __host__ __device__ inline const int2_& operator=(const double a)       { x = (int)a;       y = 0.;             return *this; }
        __host__ __device__ inline const int2_& operator=(const int2_ a)        { x = a.x;          y = a.y;            return *this; }
        __host__ __device__ inline const int2_& operator=(const float2_ a);
        __host__ __device__ inline const int2_& operator=(const double2_ a);
};
class float2_ {
    public:
        float x;
        float y;
        __host__ __device__ float2_() : x(), y() {}
        __host__ __device__ inline const float2_& operator=(const int a)        { x = (float)a;     y = 0.;             return *this; }
        __host__ __device__ inline const float2_& operator=(const float a)      { x = a;            y = 0.;             return *this; }
        __host__ __device__ inline const float2_& operator=(const double a)     { x = (float)a;     y = 0.;             return *this; }
        __host__ __device__ inline const float2_& operator=(const int2_ a)      { x = (float)a.x;   y = (float)a.y;     return *this; }
        __host__ __device__ inline const float2_& operator=(const float2_ a)    { x = a.x;          y = a.y;            return *this; }
        __host__ __device__ inline const float2_& operator=(const double2_ a);
};
class double2_ {
    public:
        double x;
        double y;
        __host__ __device__ double2_() : x(), y() {}
        __host__ __device__ inline const double2_& operator=(const int a)       { x = (double)a;    y = 0.;             return *this; }
        __host__ __device__ inline const double2_& operator=(const float a)     { x = (double)a;    y = 0.;             return *this; }
        __host__ __device__ inline const double2_& operator=(const double a)    { x = a;            y = 0.;             return *this; }
        __host__ __device__ inline const double2_& operator=(const int2_ a)     { x = (double)a.x;  y = (double)a.y;    return *this; }
        __host__ __device__ inline const double2_& operator=(const float2_ a)   { x = (double)a.x;  y = (double)a.y;    return *this; }
        __host__ __device__ inline const double2_& operator=(const double2_ a)  { x = a.x;          y = a.y;            return *this; }
};
__host__ __device__ inline const int2_& int2_::operator=(const float2_ a)       { x = (int)a.x;     y = (int)a.y;       return *this; }
__host__ __device__ inline const int2_& int2_::operator=(const double2_ a)      { x = (int)a.x;     y = (int)a.y;       return *this; }
__host__ __device__ inline const float2_& float2_::operator=(const double2_ a)  { x = (float)a.x;   y = (float)a.y;     return *this; }
int它正确地定义了, float,double和int2_,float2_和之间所有可能的赋值double2_。
我现在想重载强制转换()运算符。为了重载,例如从intto的转换,我在类float2_中添加了以下行float2_
        __host__ __device__ inline const float2_& operator()(const int in)  { x = (float)in; y=0.; return *this; }; 
不幸的是,它似乎没有效果。如果我尝试
float2_ a;
int b = 1;
a = (float2_)b;
编译器说
no suitable constructor exists to convert from "int" to "float2_"
我应该实现一个包装类,比如int_允许这种类型的转换吗?谢谢你。