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

前端 未结 10 1345
不知归路
不知归路 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 15:00

    While the other answers here that try to guess whether a class is a container or not might work for you, I would like to present you with the alternative of naming the type you want to return true for. You can use this to build arbitrary is_(something) traits types.

    template struct is_container : public std::false_type {};
    
    template 
    struct is_container> : public std::true_type {};
    
    template 
    struct is_container> : public std::true_type {};
    

    And so on.

    You will need to include and whatever classes you add to your rules.

提交回复
热议问题