I wrote a template template for an abstract container that should define numeric operators (unitary + and -, binary +, - and *) if it accepts a template parameter (that is, if it is a numeric type).
Then I would like to apply these numerical operations in containers of containers with numerical values (and on containers of containers from containers with numerical values, etc.).
I wrote the following code. The marker (A)shows how I tried to solve the problem of recursive specialization.
template <typename T>
struct is_numeric : public std::is_arithmetic<T>{};
template <typename T>
struct is_numeric<GenericContainer<T>> : public std::is_arithmetic<T>{};
template <typename T, bool isNumeric=false>
class BaseContainer : public GenericContainer<T> {};
template <typename T>
class BaseContainer<T, true> : public NumericContainer<T> {};
template <typename T>
class Container : public BaseContainer<T, is_numeric<T>::value> {};
Then in the test program I have the following statements:
typedef Vector<int, 3> V3D;
ASSERT(is_numeric<int>::value);
ASSERT(is_numeric<double>::value);
ASSERT(is_numeric<V3D>::value);
The first two statements work as expected.
Titou source
share