Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it ever make sense to overload unary operator &?

So, C++ allows overloading the unary operator &(address). Are you aware of any real-world example when operator & was rightfully overloaded? And a second, more specific question, are you aware of any real-world example when operator & was rightfully overloaded while preserving address semantics? TIA

like image 998
Armen Tsirunyan Avatar asked Oct 16 '10 11:10

Armen Tsirunyan


People also ask

Can we overload unary operator?

Overloading Unary Operator: Let us consider to overload (-) unary operator. In unary operator function, no arguments should be passed. It works only with one class objects. It is a overloading of an operator operating on a single operand.

Which unary operator Cannot be overloaded?

Operators that cannot be overloaded in C++ For an example the sizeof operator returns the size of the object or datatype as an operand. This is evaluated by the compiler. It cannot be evaluated during runtime. So we cannot overload it.

How many arguments can be passed to an overloaded unary operator?

When unary operators are overloaded through a member function, they do not take any explicit arguments. But when overloaded by a friend function, they take one argument. When binary operators are overloaded through a member function they take one explicit argument.

Should I avoid operator overloading?

One doesn't really need operator overloading in a language since they can be simulated with more verbose function calls anyway. Avoiding operator overloading in Java made the implementation and specification of Java a little simpler and it forced programmers to not abuse operators.


5 Answers

I've got 207 real-world examples of operator &(): Code search 1, Code search 2.

Including SafeInt<> (to get the underlying naked integer), boost::gil (apparently also to yield the raw data), Mozilla (that say "it is risky to define operator&, but, hey, we know what we're doing."), wxWidgets, Armagetron and lots of more.

It seems some use the iterator idiom &*it to get a raw reference or pointer backwards, and write *&it to get a raw reference and &it to get a raw pointer.

Notice that once your type overloads operator& and returns something different than the built-in operator, your type is not CopyConstructible anymore (in C++03 - C++0x seems to have lifted it), and so cannot be used as element-type in a Standard container anymore.

like image 73
Johannes Schaub - litb Avatar answered Oct 04 '22 01:10

Johannes Schaub - litb


It appears to be used in ATL, e.g http://msdn.microsoft.com/en-us/library/5s6et3yb.aspx

like image 28
UncleBens Avatar answered Oct 04 '22 03:10

UncleBens


I don't know of a concrete example off-hand, but I could imagine a container class where you might want to return a smart pointer or an iterator. I'm not saying this necessarily makes sense, though.

like image 20
Oliver Charlesworth Avatar answered Oct 04 '22 01:10

Oliver Charlesworth


One good reason to overload it might be to make it private, to prevent users from using it. I can't this think of any real-world example where you would want to prevent this, but it seems to be the most logical reason to overload it.

like image 35
Viktor Sehr Avatar answered Oct 04 '22 01:10

Viktor Sehr


I did it once when an object had a special-purpose smart pointer. operator& quietly 'lifted' a stack-allocated object into a heap-based smart pointer version, and this operator behaved differently once the object was inside the pointer.

I don't have the code any more, but there was a reason for it at the time. It's certainly not a decision to take lightly, this road is lined with corpses.

like image 44
spraff Avatar answered Oct 04 '22 01:10

spraff