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

前端 未结 10 1346
不知归路
不知归路 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:49

    template 
    struct is_container {
    
        template <
           typename U,
           typename I = typename U::const_iterator
        >   
        static int8_t      test(U* u); 
    
        template 
        static int16_t     test(...);
    
        enum { value  =  sizeof test ::type> (0) == 1 };
    };
    
    
    template  
    struct  is_container >    : std::true_type { };
    

提交回复
热议问题