lundi 1 août 2016

What is the defining quality of the indirection operator / should I not overload it as such?

I have code whereby I can query a structure for a std::set of objects of type A that all match some criteria. I will very often want my query criteria to be such that the code returns a set containing only one object. And in these cases, I will want my code to fail if the query hasn't produced only one result. So I would like to make a function

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

that throws if the set contains more than one (or no) element, and dereferences the first element otherwise.

For brevity, I thought to overload the indirection operator, which is not defined for std::set:

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

Is this a bad idea? It does fit with the concept of the indirection operator that it performs a dereferencing. But I could not find a strict definition of what the indirection operator should do according to the standards, so as to make sure whether I'm perverting its standard use (too far).

Aucun commentaire:

Enregistrer un commentaire