kebs
kebs

Reputation: 6707

sfinae to detect containers: failure for std:array

I am looking for a way to use SFINAE to implement some function, that must be available only to some containers: vector, list, array (set is there below only as a test)

Build upon this answer, I tried the code below that uses a traits class that returns true only for the required containers.

As you can see online here, it fails for std::array.

template <typename Container>
struct is_container : std::false_type { };

template <typename... Ts> struct is_container<std::array<Ts... >> : std::true_type { };
template <typename... Ts> struct is_container<std::vector<Ts...>> : std::true_type { };
template <typename... Ts> struct is_container<std::set<Ts...   >> : std::true_type { };
template <typename... Ts> struct is_container<std::list<Ts...  >> : std::true_type { };
template <typename... Ts> struct Dummy{};

int main()
{
    std::cout << "Dummy: " << is_container<Dummy<int>>::value << '\n';
    std::cout << "array: " << is_container<std::array<int,5>>::value << '\n';
    std::cout << "vector:" << is_container<std::vector<int>>::value << '\n';
    std::cout << "set: "   << is_container<std::set<int>>::value << '\n';
    std::cout << "list: "  << is_container<std::list<int>>::value << '\n';
}

What I understand is that this is due to the fact that std::array requires a second template parameter. I have a low experience with variadic templates, so my question is:

Is there a way to make this approach successful? Or shall I use another approach described in the linked question?

I'd rather have something pure C++11, but C++14 would be ok too.

Upvotes: 6

Views: 370

Answers (2)

bitmask
bitmask

Reputation: 34636

This is not SFINAE but regular template specialisation. Your std::array is not recognised because a value of type std::size_t (which ist std::array's second argument) is not a typename.

You can change your check for array specifically:

template <typename T, std::size_t N> struct is_container<std::array<T,N>> : std::true_type { };
template <typename... Ts> struct is_container<std::vector<Ts...>> : std::true_type { };

If you actually want to use SFINAE to check for anything that behaves like a container, you could check for the existance of std::begin, std::end, std::size for that type.

Upvotes: 7

songyuanyao
songyuanyao

Reputation: 172964

The problem is that the 2nd template parameter of std::array is a non-type template parameter, which doesn't match type template parameter pack typename... Ts.

You can change the specialization for std::array to:

template <typename T, std::size_t S> struct is_container<std::array<T, S>> : std::true_type { };

Upvotes: 5

Related Questions