When to use addressof(x) instead of &x?

user541686 picture user541686 · Feb 11, 2013 · Viewed 12.1k times · Source

How do I decide whether I need addressof(x) instead of &x when taking the address of an object?


Seems like the question was confusing, so a clarification is in order:

addressof obviously bypasses the overloaded address-of operator. I'm already aware of that.

What I want to know is:
How do I know if that's what I really want to do? (Especially when inside a template, etc.)

Is there some kind of "rule" that helps me figure out when I need addressof instead of &?
After all, they both return the "address of" the object, so when do I use which?

Answer

Nicol Bolas picture Nicol Bolas · Feb 11, 2013

You use std::addressof when you have to. Sadly, "when you have to" includes anytime you are working in template code and want to turn a variable of unknown type T or T& into an honest-to-God pointer to that variable's memory.

Because the C++ committee foolishly allowed the overloading of the reference operator (to little legitimate purpose), it is possible for a user to instantiate your template with some type that you can't use the reference operator to get an actual pointer to. std::addressof is a way to work around users who use this dubious C++ feature in order to do what the language should have guaranteed to work to begin with.

In short, it's a library fix for a language stupidity. Use it in template code instead of & if you want to make sure users can't break your code. If your users can be trusted not to use this ill-conceived feature, then you can use &.