我使用以下代码段测试[expr.static.cast] / 2(请参阅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';
}
输出结果为:
0x7fff65a8f6d0
0x7fff65a8f6d0
0x7fff65a8f6d0
1
在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';
为什么编译器强制将下面的最后一次转换强制为bool值?