void main(){
class C{
bool delegate( int) opIndex;
}
auto var= new C;
assert( var[ 0]); // Error: no `[]` operator overload for type `main.C`
}
In 20. the specs declare overloads to be rewrites "into calls to specially named members". In 20.8.1 the specs declare `opIndex` to be such a special name. No further restriction on the type of that member is given.
The code above declares `opIndex` to be a variable for a delegate, but compilation is refused.
Therefore either the specs are not complete or the code above should compile: code should even then compile, when the type of that variable is a class containing an appropriate overload for a call.
Comment #1 by svv1999 — 2021-04-01T10:57:20Z
Specs.20.8|1 require `opIndex` to be a method. Therefore this is not a bug, but an enhancement.
Although that requirement can be satisfied by declaring
class C{
bool delegate( int) opIndexVar;
bool opIndex( int arg){
return opIndexVar( arg);
}
}
the result would be an additional call unless the machine code generator optimizes that call away.
Comment #2 by robert.schadek — 2024-12-13T19:15:41Z