1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
When using the function template tt(Binary operator+(Binary const &lhs, Binary
const &rhs)), however, we may encounter a subtle and unexpected
complication. Consider the following program. When run, it displays the value
12, rather than 1:
verb( enum Values
{
ZERO,
ONE
};
template <typename Tp>
Tp operator+(Tp const &lhs, Tp const &rhs)
{
return static_cast<Tp>(12);
};
int main()
{
cout << (ZERO + ONE); // shows 12
})
This complication can be avoided by defining the operators in their own
namespace, but then all classes using the binary operator also have to be
defined in that namespace, which is not a very attractive
restriction. Fortunately, there is a better alternative: using the CRTP
(cf. section ref(STATICPOLY)).
|