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?