std::tuple member by member comparison fails

假装没事ソ 提交于 2019-12-24 19:02:27

问题


I wanted to test this very interesting answer and came out with this minimal implementation:

class A
{
    enum M { a };
    std::tuple<int> members;

public:

    A() { std::get<M::a>(members) = 0; }
    A(int value) { std::get<M::a>(members) = value; }
    A(const A & other) { members = other.members; }

    int get() const { return std::get<M::a>(members); }

    bool operator==(A & other) { return members == other.members; }
};

and a simple test:

int main() {

    A x(42);
    A y(x);

    std::cout << (x==y) << std::endl;

    return 0;
}

Everything's fine, until I define a simple struct B {}; and try to add an instance of it as a member. As soon as I write

std::tuple<int, B> members;

the operator== isn't ok anymore and I get this message from the compiler (gcc 5.4.1):

error: no match for ‘operator==’ (operand types are ‘std::__tuple_element_t<1ul, std::tuple<int, B> > {aka const B}’ and ‘std::__tuple_element_t<1ul, std::tuple<int, B> > {aka const B}’)
  return bool(std::get<__i>(__t) == std::get<__i>(__u))
                                 ^

I tried providing an operator== to B:

struct B
{
    bool operator==(const B &){ return true; }
};

and had an extra from compiler:

candidate: bool B::operator==(const B&) <near match>
     bool operator==(const B &){ return true; }
          ^

Can anyone explain what's wrong with B struct? Is it lacking something, or else?


回答1:


It's ultimately const correctness. You didn't const qualify B's (or A's, for that matter) comparison operator and its parameter consistently.

Since tuple's operator== accepts by a const reference, it cannot use your const-incorrect implementation. And overload resolution fails as a consequence.

Tidying up all of those const qualifiers resolves all the errors.



来源:https://stackoverflow.com/questions/49129831/stdtuple-member-by-member-comparison-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!