私は自分のアプリケーションに 3 つのクラスを定義しています: int2_
(integers のカップル)、float2_
(float のカップル)、およびdouble2_
(doubles のカップル) は、本質的に複雑な算術演算を実行するためのものです。
以下の議論の後:
と
次のソリューションを実装しました
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_
。
()
キャスト演算子をオーバーロードしたいと思います。たとえば、からint
へのキャストをオーバーロードするために、次の行をクラス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_
この種のキャストを可能にするために、ラッパー クラスを実装する必要がありますか? ありがとうございました。