Please consider the following code snippet:
template<class E>
class vector_expression
{};
template<class Tuple>
class vector
: public vector_expression<vector<Tuple>>
{};
template<class E1, class E2, class BinaryOperation>
class vector_binary_operation
: public vector_expression<vector_binary_operation<E1, E2, BinaryOperation>>
{
public:
vector_binary_operation(E1&& e1, E2&& e2, BinaryOperation op)
: m_e1(e1), m_e2(e2),
m_op(op)
{}
private:
E1 m_e1;
E2 m_e2;
BinaryOperation m_op;
};
template<class E1, class E2>
vector_binary_operation<E1, E2, std::plus<>> operator+(E1&& e1, E2&& e2) {
return{ std::forward<E1>(e1), std::forward<E2>(e2), std::plus<>{} };
}
The code above ensures that vector_binary_operation
stores references to named objects and makes copies for temporaries. The problem is the interface of operator+
, cause it actually defines this operator for any type. What do I need to change, if I want to preserve the functionality, but only define the operator for types being or derived from vector_expression
?