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

前端 未结 6 946
北恋
北恋 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 04:51

    I must admit, I don't see the motivation behind this, particularly if it requires writing shedloads of supporting code. For your operator:

    template
    bool operator==(Manager m1, Manager m2) {
        return p1.internal_field == p2.internal_field;
    }
    

    to compile without a warning, both template parameter types must be capable of being parameters to the Manager template, and those types must have private members (I assume p1 & p2 should be m1 & m2) called internal_field. Given those constraints, what is the chance that this template function can be called by accident on the wrong type(s)?

提交回复
热议问题