Why can I use operator= but not operator== with C++11 brace-initializers?

List-initialization cannot be used as an argument to an operator in the general case. Per Paragraph 8.5.4/1 of the C++11 Standard:

[…] List-initialization can be used

— as the initializer in a variable definition (8.5)

— as the initializer in a new expression (5.3.4)

— in a return statement (6.6.3)

— as a for-range-initializer (6.5)

as a function argument (5.2.2)

— as a subscript (5.2.1)

— as an argument to a constructor invocation (8.5, 5.2.3)

— as an initializer for a non-static data member (9.2)

— in a mem-initializer (12.6.2)

on the right-hand side of an assignment (5.17)

The last item explains why list-initialization is allowed on the right side of operator =, even though it is not allowed in general for an arbitrary operator.

Because of the fifth item above, however, it can be used as an argument to a regular function call, this way:

if (a.operator == ({1, 2}))

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)