CRTP and templated expressions
In a complex library that uses templated expressions, and the Curiously Recursive Template Pattern (CRTP), I need some overloaded operators to be specialized with base classes, but operations involving their derived classes are not finding the base-class specialization.
Ie:
- if an operator is defined for BaseA< T > + BaseA< T >, then code DerivedA< T > + DerivedA< T > finds the operator no problem.
- if an operator is defined for BaseB< T > + BaseB< T >, then code DerivedB< T > + DerivedB< T > finds the operator no problem.
- but if an operator is defined for BaseB< BaseA< T > > + BaseB< BaseA< T > >, then DerivedB< DerivedA< T > > + DerivedB< DerivedA< T > > is NOT finding that operator.
How can I make sure the operator for the specialized, nested case is found?
I can restate the problem thus:
If I have classes (using 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>;
and I have an operator
template<class derived1, class derived2, class datatype>
operator+(const BaseB<derived1,datatype> &bb1,const BaseB<derived2,datatype> &bb2);
it will happily be used to solve the function DerivedB< DerivedA< double > > + DerivedB< DerivedA< double> >, eg
DerivedB<DerivedA<double> > A;
DerivedB<DerivedA<double> > B;
A+B;
but if I have a more specialized operator for the same operation instead
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);
this operator is not found by the same function
DerivedB<DerivedA<double> > A;
DerivedB<DerivedA<double> > B;
A+B;
How can I ensure the specialized operator will be found for solving this function?
I have attached mimalistic code to reproduce the problem, only the one line BA1+BA2; does not compile with g++.
Full code example:
//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 mat开发者_如何转开发ch 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;
}
This is because the argument type, DerivedB<DerivedA<double> >
is not a derived class of BaseB<bderived1, BaseA<aderived1,datatype> >
: The arguments of operator+
have for their base class's second template argument passed the type DerivedA<double>
(which is datatype
) but the operator+
's function parameter specifies BaseA<aderived1,datatype>
as second template argument.
Since with these many types in there it's quite convoluted, let's make a simplier example
template<typename T>
struct B { };
template<typename T>
struct D : B<T> { };
struct base { };
struct derived : base { };
Now, let's see how these behave
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
}
The C++ Standard specifies that derived->base conversions are considered when matching a deduced function parameter type. That's why file << "hello"
works: The operator is defined in terms of basic_ostream<C,T>
even though file
really could be an basic_fstream
(derived class of it). This applies in your first case. But in your second case you try to deduce a parameter that's entirely not a base class of the argument passed.
精彩评论