Files
2025-10-25 03:02:53 +03:00

38 lines
1.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[over.sub]
# 12 Overloading [[over]](./#over)
## 12.4 Overloaded operators [[over.oper]](over.oper#over.sub)
### 12.4.5 Subscripting [over.sub]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3650)
A [*subscripting operator function*](#def:operator_function,subscripting "12.4.5Subscripting[over.sub]") is a member function named operator[] with an arbitrary number of parameters[.](#1.sentence-1)
It may have default arguments[.](#1.sentence-2)
For an expression of the form
[*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1General[expr.post.general]") [ [*expression-list*](expr.post.general#nt:expression-list "7.6.1.1General[expr.post.general]")opt ]
the operator function
is selected by overload resolution ([[over.match.oper]](over.match.oper "12.2.2.3Operators in expressions"))[.](#1.sentence-3)
If a member function is selected,
the expression is interpreted as
[*postfix-expression*](expr.post.general#nt:postfix-expression "7.6.1.1General[expr.post.general]") . operator [] ( [*expression-list*](expr.post.general#nt:expression-list "7.6.1.1General[expr.post.general]")opt )
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L3667)
[*Example [1](#example-1)*: struct X { Z operator[](std::initializer_list<int>);
Z operator[](auto...);};
X x;
x[{1,2,3}] = 7; // OK, meaning x.operator[]({1,2,3}) x[1,2,3] = 7; // OK, meaning x.operator[](1,2,3)int a[10];
a[{1,2,3}] = 7; // error: built-in subscript operator a[1,2,3] = 7; // error: built-in subscript operator — *end example*]