运算符在自定义类中定义的复数之间重载

时间:2013-09-27 15:58:58

标签: c++ class operator-overloading complex-numbers

我已按以下方式定义了我自己的复杂类float2_

class float2_ {

    public:
        float2 c;

        // Member functions
}

float2是一个CUDA struct,基本上是几个实部和虚部。同样,我定义了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+intfloatdoubleint2_的所有可能组合之间正确实施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_);

编辑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); };

BBnamespace,其中定义了复杂类型。

1 个答案:

答案 0 :(得分:1)

您编写了一个未定义模板的显式实例化。你可能想写一个完整的专业化:

template<>
float2_::float2_ operator+<int2_::int2_, float2_::float2_, float2_::float2_>(
    const int2_::int2_,const float2_::float2_) 
{
    // ...
}
但是,我认为,任何调用这些专业化的东西实际上都需要查看专业化的声明。即使这个定义没问题,也不会有太多乐趣:无法推断返回类型,即,在调用此运算符时需要显式指定返回类型。我怀疑这是你打算做的。解决第二个问题的方法可能是某种traits类,它根据参数类型确定结果类型。为了解决第一个问题,我实现了委托给另一个函数的通用模板,并在翻译单元中明确地实例化它。

处理返回类型可能如下所示:

template <typename, typename> struct result_type;
template <typename T0, typename T1>
struct result_type<int2_::int2_, float2_::float2_> {
    typedef float2_::float2_ type;
};
// other combinations

template <typename T0, typename T1>
typedef typename result_type<T0, T1>::type
operator+(T0, T1);

......翻译单元的实现可能如下所示:

float2_::float2_ add(int2_::int2_ o0, float2_::float2_ o1) {
    ...
}
// more overload of add()

template <typename T0, typename T1>
typedef typename result_type<T0, T1>::type
operator+(T0 o0, T1 o1) {
    return add(o0, o1);
}
template float2_::float2_ operator+<int2_::int2_, float2_::float2_>(
    int2_::int2_, float2_::float2_);
// more explicit instantiations
相关问题