How to write a type trait `is_container` or `is_vector`?

前端 未结 10 1342
不知归路
不知归路 2020-11-27 14:11

Is it possible to write a type trait whose value is true for all common STL structures (e.g., vector, set, map, ...)?

To get s

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 14:48

    The way I like to detect whether something is a container is to look for data() and size() member functions. Like this:

    template 
    struct is_container : std::false_type {};
    
    template 
    struct is_container().data())
          , decltype(std::declval().size())>> : std::true_type {};
    

提交回复
热议问题