8

I'm testing [expr.static.cast]/2 with the following snippet (see live example):

#include <iostream>
struct B{ int i = 2; };
struct D: public B{};
int main()
{
    D d;
    std::cout << &d << '\n';
    std::cout << &(static_cast<D&>((B&)d)) << '\n';
    std::cout << &(static_cast<const D&>((B&)d)) << '\n';
    std::cout << &(static_cast<const volatile D&>((B&)d)) << '\n';
}

The output is:

0x7fff65a8f6d0
0x7fff65a8f6d0
0x7fff65a8f6d0
1

With the following warning in gcc:

main.cpp: In function 'int main()':
main.cpp:13:57: warning: the address of 'd' will always evaluate as 'true' [-Waddress]
     std::cout << &(static_cast<const volatile D&>((B&)d)) << '\n';

Why does the compiler force the last conversion below into a bool value?

rocambille
  • 15,398
  • 12
  • 50
  • 68
Alexander
  • 2,581
  • 11
  • 17
  • 2
    Funny expermient : making `d` `volatile` also yields `true` for `&d`. – Quentin Sep 29 '16 at 14:26
  • 3
    To avoid forcing people to click on the link, please copy the warning and the runtime output into the question. – Kyle Strand Sep 29 '16 at 14:30
  • Already answered [here](http://stackoverflow.com/questions/8239262/why-is-the-address-of-this-volatile-variable-always-at-1). There is simply no conversion for volatile pointers. – midor Sep 29 '16 at 14:33

0 Answers0