d - Is opIndexAssign possible in C++? -
the d programming language version 2 has nifty method overload expression this:
classinstance[somename] = somevalue;
or 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 possible in c++ (ideally without using stl)? if so, how?
normally, use proxy object return type of operator[]
; object have custom operator=
defined. vector<bool>
specialization in c++ standard library uses proxy behavior looking for. proxy-based solution isn't transparent d version, though. code like:
class proxy; class my_map { public: proxy operator[](const key_type& k); // rest of class }; class proxy { my_map& m; key_type k; friend class my_map; proxy(my_map& m, const key_type& k): m(m), k(k) {} public: operator value_type() const {return m.read(k);} proxy& operator=(const value_type& v) {m.write(k, v); return *this;} }; proxy my_map::operator[](const key_type& k) { return proxy(*this, k); }
Comments
Post a Comment