This is the code from WebKit:
class ExecState : public Register 
{
 JSValue calleeAsValue() const { return this[JSStack::Callee].jsValue(); } 
 ... 
}
JSStack::Callee is const, Operator[] is not overloading in ExecState or Register, 
So what's the syntax in c++ of this[JSStack::Callee] ?
Well, this is a pointer to an ExecState and using the subscript operator with a pointer makes it behave as if it is an array. That is, the expression this[JSStack::Callee] accesses an object which is JSStack::Callee elements away from this. Of course, this can only possibly work if the element is a member of an array of ExecState objects.
Below is a quick stand-alone demo of using this "feature". In general I'd recommend against using it but there may be very specific needs where it is known that a type is used within an array and the accesses are viable. For example, if the type is defined locally, all known uses could be known (I'd add a comment stating that assumption, though).
#include <iostream>
class foo {
    int d_value;
public:
    foo(int i): d_value(i) {}
    int get(int i) const { return this[i].d_value; }
};
template <typename T, int Size>
int size(T(&)[Size]) { return Size; }
int main()
{
    foo f[] = { 9, 1, 8, 2, 7, 3, 6, 4, 5 };
    for (int i=0; i < size(f) - 2; ++i) {
        std::cout << "f[" << i << "].get(2)=" << f[i].get(2) << '\n';
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With