Namespaces
Variants
Views
Actions

Talk:cpp/memory/addressof

From cppreference.com

The "possible implementation" suffers by comparison with Boost's.

See http://ideone.com/7aGun5 for an example of a class for which this code will not return the address. The issue is that the C-style cast `(char&)` will call `T::operator char &` if it exists.

good catch, I guess the whole const_cast<char&> + reinterpret_cast<const volatile char&> dance found in stdlibc++ is unavoidable. --Cubbi 07:08, 7 December 2012 (PST)
Thanks for doing the edit. FYI, it came up on StackOverflow [1]. 81.110.180.57 06:51, 8 December 2012 (PST)
libc++ version is more cutted: return (T*)&reinterpret_cast<const volatile char&>(x); Ruslo 14:10, 17 August 2013 (PDT)

The example results in the same address for both cases/calls. If you add a dummy member to class Ptr before member 'data' then each case/call will print a different address. I think it will be clearer in this way. 200.114.213.51 14:35, 18 June 2013 (PDT)

Yep, noticed it's too. Add a pad to example. Ruslo 14:10, 17 August 2013 (PDT)

I think we should remove the note about suffering in comparison to boost. The stack overflow posts can't seem to agree that the boost code is correct (or useful) and both libstdc++ and libc++ use implementations that "suffer by comparison" to boost. Ericwf (talk) 16:04, 9 April 2014 (PDT)

there is no mention of boost on the page (the "suffering" comment on the Talk page referred to a different, erroneous possible implementation, removed back in december 2012). The current possible implementation has implementation-defined behavior when applied to a function, for which the linked SO post shows one possible solution. --Cubbi (talk) 16:44, 9 April 2014 (PDT)