Std :: tuple member compare member not working

I wanted to check out 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 is fine until I define a simple one struct B {};and try to add an instance of it as a member. As soon as i write

std::tuple<int, B> members;

operator== no longer works, and I get this message from the compiler (gcc 5.4.1):

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

I tried providing operator==before B:

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

and had an additional compiler:

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

Can someone explain what happened to the B structure? Is he missing something, otherwise?

0
source share
1 answer

, , . const B ( A 's, ) .

tuple operator== const, , . , , .

.

+5

Source: https://habr.com/ru/post/1694520/


All Articles