See this example:
struct Foo
{
int a;
int b;
bool operator == (const Foo & x)
{
return a == x.a && b == x.b;
}
};
int main ()
{
Foo a;
a = {1, 2};
if (a == {1, 2}) // error: expected primary-expression before ‘{’ token
{
}
}
The line a={1,2}
is fine. The braces are convert to a Foo
to match the argument type of the implicit operator=
method. It still works if operator=
is user-defined.
The line if (a=={1,2}})
errors as indicated.
Why does the expression {1,2}
not convert to a Foo
to match the user-defined operator==
method?
operator==
in your code. Did you mean "user-defined"?if (a == Foo{1, 2})
instead.