C++: How to require that one template type is derived from the other

前端 未结 6 948
北恋
北恋 2020-11-30 04:42

In a comparison operator:

template
bool operator==(Manager m1, Manager m2) {
    return m1.internal_field == m2         


        
6条回答
  •  暖寄归人
    2020-11-30 05:07

    You can use boost's typetraits (is_base_of), and boost's enable_if.

    #include 
    #include 
    
    template 
    struct has_derived_base_relationship :
        boost::integral_constant<
            bool, boost::is_base_of::value || boost::is_base_of::value 
        >
    {};
    
    template
    typename boost::enable_if, bool>::type 
    operator==(Manager m1, Manager m2) {
        return p1.internal_field == p2.internal_field;
    }
    

    On the other hand, why would operator== usage have more value with types of the same inheritance tree? Wouldn't it have to use double dispatch to achieve meaningful results?

提交回复
热议问题