Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why forbid "braced-init-list" in the built-in "operator[]"?

I just noticed, that in N3291 a change is marked (5.2.1 Subscripting [expr.sub]):

Before, it was ok to overload operator[] with the new braced-init-list:

struct X {
    Z operator[](std::initializer_list<int>);
};
X x;
x[{1,2,3}] = 7; // OK: meaning x.operator[]({1,2,3})

Now that is removed and replaced with:

A braced-init-list shall not be used with the built-in subscript operator.

What was the problem?

like image 829
towi Avatar asked Dec 22 '25 23:12

towi


1 Answers

x[{1,2,3}] is not the built-in [] operator. It invokes a user defined operator function. So take a deep breath and go on using this fun syntax.

like image 145
Johannes Schaub - litb Avatar answered Dec 29 '25 13:12

Johannes Schaub - litb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!