[Request] support subscript `[]`operator overloading
See original GitHub issueRecently operator overloading was added, operations like +, -, *, /
amongst other things were added. I noticed however that []
was not a part of this change, and was wondering if it were possible to add.
Basically, it should functions basically how get/set works, for list, map, and other custom objects, accepting the key type and either returning a value or set the value.
List<String> strList = ...;
strList[0]; // List#get(int)
strList[0] = "Hello"; // List#set(int, String)
Map<String, Value> strMap = ...;
strMap["Put"] = new Value(...); // Map#put(String, Value)
strMap["Get"]; // Map#get(String)
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
14.9 — Overloading the subscript operator - Learn C++
The subscript operator is one of the operators that must be overloaded as a member function. An overloaded operator[] function will always take ......
Read more >Overloading Subscript or array index operator [] in C++
The Subscript or Array Index Operator is denoted by '[]'. This operator is generally used with arrays to retrieve and manipulate the array ......
Read more >Subscripting [] Operator Overloading in C++ - Tutorialspoint
The subscript operator [] is normally used to access array elements. This operator can be overloaded to enhance the existing functionality of C++...
Read more >Overloading subscript operator for non-array elements
As stated in the reference, std::vector<bool> it returns a proxy class that has its operators overloaded to act as an element of the...
Read more >Operator Overloading, C++ FAQ - Standard C++
Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes). Overloaded operators are syntactic sugar for function ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Frontear Several additional operators are under consideration, these include:
++
and--
+=
,*=
, etc.[]
,[] =
If implemented,a[x]
compiles asa.get(x)
, anda[x] = b
compiles asa.set(x, b)
. Note you could add extension methods to existing classes to satisfyget
/set
methods e.g., addset(K, V)
toMap
, which delegates toput(K, V)
.Release 2020.1.34 implements operator overloading for:
++
and--
+=
,-=
,*=
,/=
,%=
,[]
, both access and assignmentPlease see Operator Overloading documentation for details.