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

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

    Why not do something like this for is_container?

    template 
    struct is_container : std::false_type { };
    
    template  struct is_container > : std::true_type { };
    template  struct is_container > : std::true_type { };
    // ...
    

    That way users can add their own containers by partially-specializing. As for is_vector et-al, just use partial specialization as I did above, but limit it to only one container type, not many.

提交回复
热议问题