float2_
次の方法で独自の複雑なクラスを定義しました
class float2_ {
public:
float2 c;
// Member functions
}
float2
は CUDAstruct
であり、基本的に実部と虚部のカップルです。同様に、クラスを定義int2_
しました。double2_
operator+
ここで、実数/複素数と複素数/複素数の可能なすべての組み合わせをオーバーロードしたいと思います。さらに、ヘッダー ファイルでは、これらすべての可能性を明示的に宣言することを避けるために、テンプレートを使用したいと考えています。
それで、私が試したのは次のとおりです。
// .cuh file
template<class A, class B, class C> C operator+(const A,const B);
// .cu file
template float2_::float2_ operator+(const int2_::int2_,const float2_::float2_)
{ // implementation };
....
しかし、これは私に次のエラーメッセージを返します:
Operator_Overloads.cu(65): error: this declaration has no storage class or type specifier
Operator_Overloads.cu(65): error: invalid explicit instantiation declaration
私の質問は、* operator+
、、、、、および?int
float
double
int2_
float2_
double2_
数の加算の組み合わせが異なると実装が異なるため、実装を「テンプレート化」することはできないことに注意してください。
どうもありがとうございました。
編集 - DIETMAR KUEHL の提案に基づく暫定的な解決策
// Include file
template<typename T0, typename T1, typename T2> T2 operator+(T0, T1);
// --- Translational unit
// --- Auxiliary function add --- complex/complex
BB::float2_ add(const BB::int2_ a, const BB::float2_ b) { BB::float2_ c; c.c.x = (float)a.c.x + b.c.x; c.c.y = (float)a.c.y + b.c.y; return c; };
// --- Template definition of operator+
template <typename T0, typename T1, typename T2> T2 operator+(T0 o0, T1 o1) { return add(o0, o1); }
// --- Instantiation of operator+
template BB::float2_ operator+<BB::int2_, BB::float2_>(BB::int2_, BB::float2_);
EDIT 2 - ワーキングソリューション
// --- Include file
template <typename, typename> struct result_type;
template <>
struct result_type<BB::int2_, BB::float2_> {
typedef BB::float2_ type;
};
template<typename T0, typename T1> typename result_type<T0, T1>::type operator+(T0, T1);
// --- Translational unit
BB::float2_ add(const BB::int2_ a, const BB::float2_ b) { BB::float2_ c; c.c.x = (float)a.c.x + b.c.x; c.c.y = (float)a.c.y + b.c.y; return c; };
BB::float2_ operator+(BB::int2_ a, BB::float2_ b) { return add(a, b); };
BB
はnamespace
、複合型が定義されている場所です。