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.
c++11
tag but it was interleaved with another edit that added already the max 5 tags. Imho it would be nice to have thec++11
tag but didnt want to decide what else to remove – Finnell