Disabling operators overloads if a given template is not specialized for the operator parameters type

蹲街弑〆低调 提交于 2020-01-06 01:18:10

问题


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.


回答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).



来源:https://stackoverflow.com/questions/18688678/disabling-operators-overloads-if-a-given-template-is-not-specialized-for-the-ope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!