Ale*_*der 8 c++ static-cast c++14
我正在使用以下代码片段测试[expr.static.cast]/2(请参阅实例):
#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';
}
Run Code Online (Sandbox Code Playgroud)
输出是:
0x7fff65a8f6d0
0x7fff65a8f6d0
0x7fff65a8f6d0
1
Run Code Online (Sandbox Code Playgroud)
在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';
Run Code Online (Sandbox Code Playgroud)
为什么编译器将下面的最后一次转换强制为bool值?