Cannot trait an inherited class as base with SFINAE
c++, c++11, inheritance, sfinae, traits
Solution
You could use `std::is_base_of`:
template <typename C>
struct container_traits {
constexpr static bool is_array = false;
};
template <template <typename, std::size_t> class C,
typename T, std::size_t N>
struct container_traits<C<T,N>> {
constexpr static bool is_array = std::is_base_of<std::array<T,N>, C<T,N>>::value;
};
See it in action.
It's basically specialized for any template type that takes two parameters. And in that specialization, the value of `is_array` is determined by `std::is_base_of`.
Problem
I create a `container_traits` class to check if a container is `std::array` or not. But it fails to catch a container inherited from `std::array`. Any solutions? ``` #include <vector> #include <array> #include <iostream> using namespace std; template<typename C> struct container_traits { constexpr static bool is_array = false; }; template<typename T, size_t S> struct container_traits<std::array<T,S>> { constexpr static bool is_array = true; }; template<typename T, size_t S> struct A : public std::array<T,S> {}; int main() { cout << container_traits<A<int, 5>>::is_array << endl; // must return 1 cout << container_traits<std::array<int, 10>>::is_array << endl; // must return 1 cout << container_traits<std::vector<int>>::is_array << endl; // must return 0 return 0; } ```