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
- 1,040,783
- 362
- 3,548
- 3,513
- 125,569
- 56
- 315
- 427
-
@Johannes: Thanks! I never knew about google code search. It's a pity I can't accept a comment as the answer :) – Armen Tsirunyan Oct 16 '10 at 13:43
6 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.
- 481,675
- 123
- 870
- 1,191
It appears to be used in ATL, e.g http://msdn.microsoft.com/en-us/library/5s6et3yb.aspx
- 39,805
- 6
- 53
- 90
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.
- 260,367
- 30
- 546
- 667
-
-
You're right Oli. Smart pointer are classes which have to behave like pointers and thus need operators * or even & overloaded. Bang on. – Samrat Patil Oct 16 '10 at 11:28
-
2@Samrat: What on earth does overloading & for a smart-pointer give you?! – Armen Tsirunyan Oct 16 '10 at 11:31
-
If it doesn't make any sense please point that out. Although, I'm for sure, operator* is overloaded in smart pointer classes. This is the best scenario I can come up with for operator& :) – Samrat Patil Oct 16 '10 at 11:49
-
1@Samrat: I imagine that works, but it's slightly annoying semantically. Consider that `*&pObj` doesn't get you back to where you started! – Oliver Charlesworth Oct 16 '10 at 12:14
-
1@Samrat: With the same logic one must overload operator& for ANY proxy obect, including iterators... which I hope you agree is insane. And Oli's argument is pretty strong too – Armen Tsirunyan Oct 16 '10 at 12:46
-
Actually, to be fair, `std::vector
v; &*v.begin();` doesn't get you back to where you started, either! – Oliver Charlesworth Oct 16 '10 at 17:09
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.
- 12,595
- 3
- 54
- 87
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.
- 30,806
- 22
- 110
- 214
I overloaded this operator when writing classes for interacting with Direct3D. It was a smart pointer class that needed to have T** returned from operator& so that it could be used in functions that expect pointer-to-pointer. T** semantics are rare but you do need them in some situations.
- 141,834
- 35
- 244
- 454