1

I'm writting a metaprogramming library which includes a set of compile-time arithmetic types and functions. For example:

metafunctions.hpp

template<typename T>
struct function
{
    using result = T;
};

template<typename LHS , typename RHS>
struct add_t;

//Add metafunction
template<typename LHS , typename RHS>
using add = typename add_t<LHS,RHS>::result;

fixed_point.hpp:

#include "metafunctions.hpp"

template<long long int BITS , unsigned int PRECISSION>
struct fixed_point
{
    operator float()
    {
        return (float)BITS * std::pow(10.0f,-(float)PRECISION); //Its implemented as decimal fixed_point, not binary.
    };
};

//An alias which provides a convenient way to create numbers: scientific notation
template<int mantissa , int exponent = 0 , fbcount PRECISSION = mpl::DEFAULT_FRACTIONAL_PRECISION> // MANTISSA x 10^EXPONENT
using decimal = mpl::fixed_point<decimal_shift<mantissa , PRECISSION + exponent>::value , PRECISSION>; 

//add specialization:
template<long long int BITS1 , long long int BITS2 , unsigned int PRECISSION>
struct add_t<mpl::fixed_point<BITS1,PRECISION> , mpl::fixed_point<BITS2,PRECISION>> : public mpl::function<fixed_point<BITS1+BITS2 , PRECISION>> {};

A few days ago I have noticed that I could take advantage of decltype keyword and implement expression templates to simplify the syntax of complex expressions:

expressions.hpp

template<typename LHS , typename RHS>
mpl::add<LHS,RHS> operator+(const LHS& , const RHS&);

template<typename LHS , typename RHS>
mpl::sub<LHS,RHS> operator-(const LHS& , const RHS&);

template<typename LHS , typename RHS>
mpl::mul<LHS,RHS> operator*(const LHS& , const RHS&);

template<typename LHS , typename RHS>
mpl::div<LHS,RHS> operator/(const LHS& , const RHS&);

An example of its usage:

using pi = mpl::decimal<3141592,-6>; //3141592x10⁻-6 (3,141592)
using r  = mpl::decimal<2,-2>; //0,002
using a  = mpl::decimal<1>; // 1,0 

using x = decltype( pi() * r() + ( r() / a() ) ); //pi*r + (r/a)

But this has a downside: That overloads are defined for any type, so expressions like "a string" + "other string" fits in the expressions.hpp operator+ overload. Of course that overload resolution results in a compilation error, because the compiler is trying to fit std::string as type parameters of mpl::add_t.

My question is: Is there any way to disable/enable the overloads (with std::enable_if or something like that) based on the existence of a specialization of an specific template (mpl::add_t in the example) for the parameters of the functions/operators?.

So if the result type of the expression has an specialization for the types of the parameters, the operator will be enabled, and disabled in other case.

Manu343726
  • 13,969
  • 4
  • 40
  • 75

1 Answers1

1

Something like this might work:

template<typename LHS , typename RHS>
typename std::enable_if<
   !std::is_same<
       void,
       typename mpl::add_t<LHS,RHS>::result
    >::value,
    mpl::add<LHS,RHS>
>::type
operator+(const LHS& , const RHS&)
{
    // ...
}

relying on the fact that if and only if there is a specialization, a typedef add_t<LHS,RHS>::result exists (and which is not void).

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180