2

In <vadefs.h> you'll find the following definition for the macro _ADDRESSOF(v) in VS2019:

#ifdef __cplusplus
    #define _ADDRESSOF(v) (&const_cast<char&>(reinterpret_cast<const volatile char&>(v)))
#else
    #define _ADDRESSOF(v) (&(v))
#endif

I'd like to understand the first definition above, when __cplusplus is defined. Why does it work?

John Kalane
  • 1,163
  • 8
  • 17
  • 3
    Since the macro starts with an "_" it's an internal implementation detail of a library. In "C" this returns a pointer to v (address), in C++ it goes through some extra hoops to ensure a char* to the adress of v succeeds when either v has a volatile or const attribute. (Some more details here : https://stackoverflow.com/questions/2333960/why-and-when-is-cast-to-char-volatile-needed) – Pepijn Kramer Jan 07 '22 at 10:33
  • 1
    Also see: [`std::addressof`](https://en.cppreference.com/w/cpp/memory/addressof) – Ted Lyngmo Jan 07 '22 at 10:38
  • 1
    Also, unlike in C, in C++ some "smart" guy can overload `operator&` for some classes, to return something other than the address. However, type `char` uses the built in operator that cannot be changed. – BoP Jan 07 '22 at 11:34

0 Answers0