I have a set of operators that I need to override for expression templating. I would like all derived classes of a base type match to the base type. Other things would then be caught by a generic type. Unfortunately, the generic type grabs the derived types before the base type does. To make things nice and confusing, everything is templated pretty heavily, including some CRTP. Let me try to give a more simple version of the code:
// Note: 'R' is used for return type
template <typename DerivedType, typename R>
class Base
{ // ...
};
template <typename E1, typename E2, typename R>
class MultOperation : public Base<MultOperation<E1, E2, R>, R>
{ // ...
};
template <typename T>
class Terminal : public Base<Terminal<T>, T>
{ // ...
};
// The broken operators:
template <typename T1, typename T2, typename R1, typename R2>
MultOperation<Base<T1, R1>, Base<T2, R2>, typename boost::common_type<R1, R2>::type>
operator*( Base<T1, R1> const& u, Base<T2, R2> const& v)
{
return MultOperation<Base<T1, R1>, Base<T2, R2>, typename boost::common_type<R1, R2>::type>(u, v);
}
template <typename T1, typename T2, typename R1, typename R2>
MultOperation<Terminal<T1>, Base<T2, R2>, typename boost::common_type<T1, R2>::type>
operator*( T1 const& u, Base<T2, R2> const& v)
{
return MultOperation<Terminal<T1>, Base<T2, R2>, typename boost::common_type<T1, R2>::type>(Terminal<T1>(u), v);
}
template <typename T1, typename T2, typename R1, typename R2>
MultOperation<Base<T1, R1>, Terminal<T2>, typename boost::common_type<R1, T2>::type>
operator*( Base<T1, R1> const& u, T2 const& v)
{
return MultOperation<Base<T1, R1>, Terminal<T2>, typename boost::common_type<R1, T2>::type>(u, Terminal<T2>, v);
}
Now, I can't use any new C++ features. (This is part of some refactors to remove old libraries so we can upgrade to the new cpp standards.) I can use boost stuff, though. I was thinking my answer might lie in boost::enable_if
stuff, but all my attempts have led to dead ends. Now, keep in mind that the goal is expression templates, so I can't do any casting stuff for data coming in. Yeah... it's so complicated... I hope you have some magic up your sleeve.
Short version of the question:
How can I get (1 * Derived) * Derived
to match to operator(T, Base)
for the first operator, then operator(Base, Base)
for the second operator?
It currently matches the first fine, then the second matches to one of the Base-generic operators instead, as T takes no conversion and thereby matches better than Base.
class Terminal : public Base<DerivedType<T>, T>
supposed to beclass Terminal : public Base<Terminal<T>, T>
? – GimmalTerminal
combined withenable_if
may hold the answer. Still working on it in cases there are unforeseen problems. – Ealasaid1 * A * B
it should match the first two to the second operator above. That will return theMultOperation
which, withB
, should match to the first operator. Currently, the second step would match to the more generic second or third operator instead. – EalasaidBase<T,R>
overloads need a pointer conversion. Do you have specializations forBase
? If not, perhaps you could provide a typedef or any sort of a tag insideBase
which will let you test if theT
inherits fromBase
and do sfinae on this. – Ilocano