fork download
  1. #include <type_traits>
  2.  
  3. template<class T>
  4. class is_container{
  5. typedef char (&two)[2];
  6.  
  7. template<class U> // non-const
  8. static auto test(typename U::iterator*, int)
  9. -> decltype(std::declval<U>().begin(), char());
  10.  
  11. template<class U> // const
  12. static auto test(typename U::const_iterator*, long)
  13. -> decltype(std::declval<U const>().begin(), char());
  14.  
  15. template<class>
  16. static two test(...);
  17.  
  18. public:
  19. static bool const value = sizeof(test<T>(0,0)) == 1;
  20. };
  21.  
  22. #include <iostream>
  23. #include <vector>
  24. #include <list>
  25. #include <set>
  26. #include <map>
  27.  
  28. int main()
  29. {
  30. std::cout << is_container<std::vector<std::string> >::value << ' ';
  31. std::cout << is_container<std::list<std::string> >::value << ' ';
  32. std::cout << is_container<std::set<std::string> >::value << ' ';
  33. std::cout << is_container<std::map<std::string, std::string> >::value << '\n';
  34. }
Success #stdin #stdout 0s 2828KB
stdin
Standard input is empty
stdout
1 1 1 1