The D Programming Language Version 2 has a nifty method to overload an expression like this:
classInstance[someName] = someValue;
Or as D Function defined in this little example:
ref Map opIndexAssign(ref const(ValueT) value, ref const(NameT) name)
{
this.insert(name, value);
return this;
}
Is this possible in C++ (ideally without using the STL)? If so, how?
Normally, you would use a proxy object as the return type of
operator[]; that object will have a customoperator=defined. Thevector<bool>specialization in the C++ Standard Library uses a proxy to get the behavior you are looking for. The proxy-based solution isn’t as transparent as the D version, though. The code is something like: