I’m developing an automatic differentiation tool based on expression templates and operator/function overloading. The template std::max function, for example, was successfully overloaded:
namespace ead {
...
template<class A>
struct ExprWrap
{
inline
operator A const& () const
{ return *static_cast<A const*>(this);}
};
class adnumber : public ExprWrap<adnumber>
{ ..... };
template<typename L, typename R>
class MaxExpr : public ExprWrap<MaxExpr<L,R> >{ ...... };
// overloading std::max
template<typename L, typename R>
MaxExpr<L,R>
max (ExprWrap<L> const& l, ExprWrap<R> const& r)
{
return MaxExpr<L,R>(l,r); // return an expression
}
...
}
but in a code like the following
using namespace std;
using namespace ead;
adnumber x,y,z;
z = max(x,y); // call std::max
std:: is used if the namespace is omitted, and for some other functions, ead:: is used. Is there a trick to force the compiler to always choose the ead:: namespace, for example, for max function? (without C++11 features, please) Why compiler thinks the std::max is a better match?
Ok, I know that to write ead:: before functions name is not a big deal, but I’d like to save the user from typing.
Consider what instantiation of
std::maxyields:This is a better match than your signature of
max(it is an exact match). Thus it will be called. Your only way out is to qualify the call, because you cannot make instantiation ofstd::maxfail through SFINAE or make youread::maxa fully genericead::max(T, T)as well. The second attempt would make the call ambiguous.