Implementation of addressof

  • First you have __r which is of type _Tp&
  • It is reinterpret_cast‘ed to a char& in order to ensure being able to later take its address without fearing an overloaded operator& in the original type; actually it is cast to const volatile char& because reinterpret_cast can always legally add const and volatile qualifiers even if they are not present, but it can’t remove them if they are present (this ensures that whatever qualifiers _Tp had originally, they don’t interfere with the cast).
  • This is const_cast‘ed to just char&, removing the qualifiers (legally now! const_cast can do what reinterpret_cast couldn’t with respect to the qualifiers).
  • The address is taken & (now we have a plain char*)
  • It is reinterpret_cast‘ed back to _Tp* (which includes the original const and volatile qualifiers if any).

Edit: since my answer has been accepted, I’ll be thorough and add that the choice of char as an intermediate type is due to alignment issues in order to avoid triggering Undefined Behaviour. See @JamesKanze’s comments (under the question) for a full explanation. Thanks James for explaining it so clearly.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)