language: C++11 (gcc-4.7.2)
date: 467 days 15 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <type_traits>
 
template<class T>
class is_container{
  typedef char (&two)[2];
 
  template<class U> // non-const
  static auto test(typename U::iterator*, int)
      -> decltype(std::declval<U>().begin(), char());
 
  template<class U> // const
  static auto test(typename U::const_iterator*, long)
      -> decltype(std::declval<U const>().begin(), char());
 
  template<class>
  static two  test(...);
 
public:
  static bool const value = sizeof(test<T>(0,0)) == 1;
};
 
#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <map>
 
int main()
{
    std::cout << is_container<std::vector<std::string> >::value << ' ';
    std::cout << is_container<std::list<std::string> >::value << ' ';
    std::cout << is_container<std::set<std::string> >::value << ' ';
    std::cout << is_container<std::map<std::string, std::string> >::value << '\n';
}