CRTP和模板化表达式

时间:2010-08-31 11:36:07

标签: c++ templates expression crtp

在使用模板化表达式和奇怪递归模板模式(CRTP)的复杂库中,我需要一些重载操作符专门用于基类,但涉及其派生类的操作不能找到基类专门化。 / p>

即:

  • 如果为BaseA<定义了运营商? T> + BaseA< T>,然后代码DerivedA< T> + DerivedA< T>发现操作员没问题。
  • 如果为BaseB<定义了运营商? T> + BaseB< T>,然后 代码DerivedB< T> + DerivedB< T>发现操作员没问题。
  • 但是如果为BaseB<定义了运营商? BaseA< T> > + BaseB< BaseA< T> >,然后 DerivedB< DerivedA< T> > + DerivedB< DerivedA< T> >找不到那个操作员。

如何确保找到专门的嵌套案例的运算符?

我可以重申这个问题:

如果我有课程(使用CRTP)

template<typename derived, typename datatype> class BaseA;
template<typename derived, typename datatype> class BaseB;
template<typename datatype> class DerivedA : public BaseA<DerivedA<datatype>,datatype>;
template<typename datatype> class DerivedB : public BaseB<DerivedB<datatype>,datatype>;

我有一个运营商

template<class derived1, class derived2, class datatype>
operator+(const BaseB<derived1,datatype> &bb1,const BaseB<derived2,datatype> &bb2);

很乐意用它来解决这个问题 DerivedB&LT; DerivedA&LT;双&gt; &GT; + DerivedB&lt; DerivedA&LT;双&GT; &gt;,例如

DerivedB<DerivedA<double> > A;
DerivedB<DerivedA<double> > B;
A+B;

但是如果我有一个更专业的操作员来代替相同的操作

template<class bderived1, class aderived1, class datatype, class bderived2, class aderived2>
operator+(const BaseB<bderived1,BaseA<aderived1,datatype> > &bb1,const BaseB<bderived2,BaseA<aderived2,datatype> > &bb2);

同一功能找不到此运算符

DerivedB<DerivedA<double> > A;
DerivedB<DerivedA<double> > B;
A+B;

如何确保找到专门的操作员来解决此功能?

我附加了mimalistic代码来重现问题,只有一行 BA1 + BA2; 不用g ++编译。

完整代码示例:

//uses templated expressions

//uses CRTP, see
//http://en.wikipedia.org/wiki/Curiously_Recurring_Template_Pattern
//http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Curiously_Recurring_Template_Pattern

//g++ problem.cpp -o problem

#include <iostream> //cout, endl
#include <stdlib.h> //EXIT_SUCCESS

using namespace std;


//TypeC
template<class datatype1, class datatype2>
class TypeC{

    public:
    TypeC(const datatype1 &d1,const datatype2 &d2){
        cout << "helloC" << endl;
    };
};

//BaseA
template <typename derived, typename datatype>
class BaseA{

};


//DerivedA
template <typename datatype>
class DerivedA : public BaseA<DerivedA<datatype>,datatype> {

};

//operator for BaseA+BaseA
template<class derived1, class derived2, class datatype>
TypeC< BaseA<derived1,datatype>,BaseA<derived2,datatype> >
operator+(const BaseA<derived1,datatype> &ba1,const BaseA<derived2,datatype> &ba2){
    return TypeC< BaseA<derived1,datatype>,BaseA<derived2,datatype> > (ba1,ba2);
};

//BaseB
template <typename derived, typename datatype>
class BaseB{

};


//DerivedB
template <typename datatype>
class DerivedB : public BaseB<DerivedB<datatype>,datatype> {


};

/*for reasons outside the scope of this example, operators for BaseB<> op BaseB<> need specialization, cant use the general case:
//operator for BaseB+BaseB
template<class derived1, class derived2, class datatype>
TypeC< BaseB<derived1,datatype>,BaseB<derived2,datatype> >
operator+(const BaseB<derived1,datatype> &bb1,const BaseB<derived2,datatype> &bb2){
    return TypeC< BaseB<derived1,datatype>,BaseB<derived2,datatype> > (bb1,bb2);
};
*/

//operator for BaseB<double>+BaseB<double>
template<class derived1, class derived2>
TypeC< BaseB<derived1,double>,BaseB<derived2,double> >
operator+(const BaseB<derived1,double> &bb1,const BaseB<derived2,double> &bb2){
    return TypeC< BaseB<derived1,double>,BaseB<derived2,double> > (bb1,bb2);
};

//operator for BaseB<BaseA>+BaseB<BaseA>
template<class derived1, class derived2, class Aderived1, class Aderived2, class datatype>
TypeC< BaseB<derived1,BaseA<Aderived1,datatype> >,BaseB<derived2,BaseA<Aderived2,datatype> > >
operator+(const BaseB<derived1,BaseA<Aderived1,datatype> > &bb1,const BaseB<derived2,BaseA<Aderived2,datatype> > &bb2){
    return TypeC< BaseB<derived1,BaseA<Aderived1,datatype> >,BaseB<derived2,BaseA<Aderived2,datatype> > > (bb1,bb2);
};




int main(int argc, char* argv[]){

    DerivedA<double> A1;
    DerivedA<double> A2;

    A1+A2; //knows this DerivedA+DerivedA is equivalent to BaseA+BaseA, hence calls "operator for BaseA+BaseA"

    DerivedB<double> B1;
    DerivedB<double> B2;

    B1+B2; //knows this DerivedB<double>+DerivedB<double> is equivalent to BaseB<double>+BaseB<double>,
    //hence calls "operator for BaseB<double>+BaseB<double>"

    DerivedB<DerivedA<double> > BA1;
    DerivedB<DerivedA<double> > BA2;

    BA1+BA2; //g++ error: no match for ‘operator+’ in ‘BA1 + BA2’
    //compiler cannot see this DerivedB<DerivedA<double> > + DerivedB<DerivedA<double> > is equivalent to BaseB<BaseA>+BaseB<BaseA>
    //I want it to see this op as equivalent to BaseB<derived1,BaseA<Aderived1,datatype> > + BaseB<derived2,BaseA<Aderived2,datatype> >
    //How can I make BaseA act as a wildcard for DerivedA and any other classes derived from it, in this nested case?

    return EXIT_SUCCESS;

}

1 个答案:

答案 0 :(得分:3)

这是因为参数类型DerivedB<DerivedA<double> >不是BaseB<bderived1, BaseA<aderived1,datatype> >的派生类:operator+的参数对于它们的基类的第二个模板参数传递了类型DerivedA<double> }(datatype},但operator+的函数参数指定BaseA<aderived1,datatype>作为第二个模板参数。

因为有这么多类型,所以很复杂,让我们做一个更简单的例子

template<typename T>
struct B { };

template<typename T>
struct D : B<T> { };

struct base { };
struct derived : base { };

现在,让我们看看这些行为

template<typename T>
void f(B<T> const&);

void g(B<base> const&);
void h(B<derived> const&);

int main() {
  D<derived> dd;
  f(dd); // works, like your first case
  h(dd); // works too (just not deduced).

  g(dd); // does *not* work, just like your second case
}

C ++标准规定在匹配推导出的函数参数类型时会考虑派生的&gt;基本转换。这就是file << "hello"有效的原因:尽管basic_ostream<C,T>实际上可能是file(派生类),但运算符仍以basic_fstream定义。这适用于您的第一个案例。但在第二种情况下,您尝试推导出一个完全不是传递参数的基类的参数。