我目前正在实现一个基于元编程的小型编译时计算库。
如果已经为运算符定义了一个基类,它有一个结果 typedef(我决定使用整数包装器,例如std::integral_constant
作为值而不是原始整数值,以提供沿库的统一接口)和一个 n 元运算符基类,检查运算符是否至少有一个操作数:
template<typename RESULT>
struct operator
{
using result = RESULT;
};
template<typename RESULT , typename... OPERANDS>
struct nary_operator : public operator<RESULT>
{
static_assert( sizeof... OPERANDS > 0 , "An operator must take at least one operand" );
};
所以我为一元和二元运算符定义了别名:
template<typename OP , typename RESULT>
using unary_operator = nary_operator<RESULT , OP>;
template<typename LHS , typename RHS , typename RESULT>
using binary_operator = nary_operator<RESULT , LHS , RHS>;
该运算符接口用于将自定义运算符定义为别名,例如下面的比较运算符:
template<typename LHS , typename RHS>
using equal = binary_operator<LHS,RHS,bool_wrapper<LHS::value == RHS::value>>;
template<typename LHS , typename RHS>
using not_equal = logical_not<equal<LHS,RHS>>;
template<typename LHS , typename RHS>
using less_than = binary_operator<LHS,RHS,bool_wrapper<LHS::value < RHS::value>>;
template<typename LHS , typename RHS>
using bigger_than = less_than<RHS,LHS>;
template<typename LHS , typename RHS>
using less_or_equal = logical_not<bigger_than<LHS,RHS>>;
template<typename LHS , typename RHS>
using bigger_or_equal = logical_not<less_than<LHS,RHS>>;
现在假设我们想为我们自己的类实现我们的自定义相等运算符。例如:
template<typename X , typename Y , typename Z>
struct vec3
{
using x = X;
using y = Y;
using z = Z;
};
如果相等运算符是在继承时创建的,而不是别名,则可以通过模板特化轻松完成:
//Equality comparator implemented through inheritance:
template<typename LHS , typename RHS>
struct equal : public binary_operator<LHS,RHS,bool_wrapper<LHS::value == RHS::value>> {};
//Specialization of the operator for vec3:
template<typename X1 , typename Y1 , typename Z1 , typename X2 , typename Y2 , typename Z2>
struct equal<vec3<X1,Y1,Z1>,vec3<X2,Y2,Z2>> : public binary_operator<vec3<X1,Y1,Z1>,vec3<X2,Y2,Z2> , bool_wrapper<X1 == X2 && Y1 == Y2 && Z1 == Z2>> {};
我知道模板别名不能专门化。
我的问题是:有没有办法不使用继承设计而不是模板别名来专门化这种模板别名?