vendredi 18 septembre 2020

Adding the : in front of variable gives wrong result

I am comparing the class. For the below code

#include <string>
#include <set>
#include <tuple>
#include <cassert>


enum class e : bool
{
    positive = true,
    negetive = false
};


class A
{
public:
    int a;
    e e1 : 1;
    
  friend bool operator==(const A&, const A&);
};

 
 bool operator==(const A& lhs, const A& rhs) {
  auto tie = [](const A& a1) {
    return std::tie(a1.a, a1.e1);
  };
  auto x1 = tie(lhs);
  auto x2 = tie(rhs);
  return x1 == x2;   
}

int main()
{
A a1;
a1.a = 10;
a1.e1 = e::positive;

A b1;
b1.a = 10;
b1.e1 = e::positive;

assert(a1 == b1);

}

Output is :

a.out: main.cpp:44: int main(): Assertion `a1 == b1' failed.

Which is wrong, as two of the classes are the same.

However, if I change the line of code from e e1 : 1; to e e1; it gives the right result.

First of I am wondering what does : does in this case? Why the result is wrong after adding this?

Code can be seen here.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire