如果给定模板不是为操作员参数类型专门化,则禁用运算符过载 [英] Disabling operators overloads if a given template is not specialized for the operator parameters type
问题描述
我在写一个元编程库,它包括一组编译时算术类型和函数。例如:
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>> {};
前几天我注意到我可以利用 decltype
关键字并实施表达式模板以简化复杂表达式的语法:
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&);
使用示例:
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)
但这有一个缺点:对于任何类型定义重载,因此一个字符串+其他字符串
的表达式适用于 expressions.hpp
运算符+
重载。当然,重载分辨率会导致编译错误,因为编译器尝试将 std :: string
作为 mpl :: add_t
。
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
.
我的问题是:是否有任何方法禁用/启用重载( std :: enable_if
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.
推荐答案
这样可能会有效:
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&)
{
// ...
}
依赖于当且仅当存在特殊化时,typedef add_t
存在(而不是 void
)。
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
).
这篇关于如果给定模板不是为操作员参数类型专门化,则禁用运算符过载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!