Search code examples
arraysc++11curly-bracesinitializer-list

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?


Solution

  • 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.