c++ - What is the defining quality of the indirection operator? -


i have code whereby can query structure std::set of objects of type a match criteria. want query criteria such code returns set containing 1 object. , in these cases, want code fail if query hasn't produced 1 result. make function

a& deref_or_throw(std::set<a> s) { if (s.size() != 1) throw ...; return *s.begin(); } 

that throws if set contains more 1 (or no) element, , dereferences first element otherwise.

for brevity, thought overload indirection operator, not defined std::set:

a& operator*(std::set<a>& s) {return deref_or_throw(s);} 

is bad idea? fit concept of indirection operator performs dereferencing. not find strict definition of indirection operator should according standards, make sure whether i'm perverting standard use (too far).

don't overload operator* in way. 1 person's brevity person's obfuscation.

in case there no precedent operator* operating on standard container in future if looks @ code have no idea without finding implementation of operator*. instead, take 10 seconds copy-paste function call name , save future maintainers knows how time hunting down operator overload year now.

i suggest maybe *ensure_single_element(your_set).begin() or it's quite clear what's going on.


Comments