有条件地启用运算符重载
Conditionally enable an operator overloading
请考虑以下代码片段:
template<class E>
class vector_expression {};
template<class Tuple>
class vector
: public vector_expression<vector<Tuple>>
{
public:
using value_type = typename Tuple::value_type;
using size_type = typename Tuple::size_type;
};
namespace detail
{
template<class E>
constexpr bool is_vector_expression_v = std::is_base_of_v<vector_expression<std::decay_t<E>>, std::decay_t<E>>;
template<class E, class = void>
struct value_type { using type = std::decay_t<E>; };
template<class E>
struct value_type<E, std::enable_if_t<is_vector_expression_v<E>>> { using type = typename std::decay_t<E>::value_type; };
template<class E>
using value_type_t = typename value_type<E>::type;
template<class E1, class E2, class BinaryOperation>
class vector_binary_operation
{
public:
using value_type = std::result_of_t<BinaryOperation(value_type_t<E1>, value_type_t<E2>)>;
};
}
如何有条件地为 vectory_binary_operation
启用 operator[](size_type i)
?这个想法是 return ...
m_op(m_e1[i], m_e2[i])
当且仅当 m_e1
和 m_e2
都是 vector_expression
s
m_op(m_e1, m_e2[i])
当且仅当 m_e1
不是 vector_expression
m_op(m_e1[i], m_e2)
当且仅当 m_e2
不是 vector_expression
我该怎么做?我试图将运算符视为 template<class F1 = E1, class F2 = E2>
并使用 std::enable_if_t<is_vector_expression_v<F1> && is_vector_expression_v<F2>
(等等)。但这会产生编译器错误消息 no type named 'type' in 'std::enable_if'; 'enable_if' 不能用于禁用此声明。
在函数或运算符上,这样使用 enable_if:
template<class A, class B,
std::enable_if_t<condition_test_here<A,B>::value>* = nullptr>
auto operator+(A, const B&)
{
...
}
请考虑以下代码片段:
template<class E>
class vector_expression {};
template<class Tuple>
class vector
: public vector_expression<vector<Tuple>>
{
public:
using value_type = typename Tuple::value_type;
using size_type = typename Tuple::size_type;
};
namespace detail
{
template<class E>
constexpr bool is_vector_expression_v = std::is_base_of_v<vector_expression<std::decay_t<E>>, std::decay_t<E>>;
template<class E, class = void>
struct value_type { using type = std::decay_t<E>; };
template<class E>
struct value_type<E, std::enable_if_t<is_vector_expression_v<E>>> { using type = typename std::decay_t<E>::value_type; };
template<class E>
using value_type_t = typename value_type<E>::type;
template<class E1, class E2, class BinaryOperation>
class vector_binary_operation
{
public:
using value_type = std::result_of_t<BinaryOperation(value_type_t<E1>, value_type_t<E2>)>;
};
}
如何有条件地为 vectory_binary_operation
启用 operator[](size_type i)
?这个想法是 return ...
m_op(m_e1[i], m_e2[i])
当且仅当m_e1
和m_e2
都是vector_expression
sm_op(m_e1, m_e2[i])
当且仅当m_e1
不是 vector_expressionm_op(m_e1[i], m_e2)
当且仅当m_e2
不是 vector_expression
我该怎么做?我试图将运算符视为 template<class F1 = E1, class F2 = E2>
并使用 std::enable_if_t<is_vector_expression_v<F1> && is_vector_expression_v<F2>
(等等)。但这会产生编译器错误消息 no type named 'type' in 'std::enable_if'; 'enable_if' 不能用于禁用此声明。
在函数或运算符上,这样使用 enable_if:
template<class A, class B,
std::enable_if_t<condition_test_here<A,B>::value>* = nullptr>
auto operator+(A, const B&)
{
...
}