我写了这段代码:
enum Color
{
kRed,
kGreen,
kBlue,
kUnexistingColor
};
const char* foo(Color color)
{
switch (color)
{
case kRed:
return "red";
case kGreen:
return "green";
case kBlue:
return "blue";
case kUnexistingColor:
default:
__builtin_unreachable();
}
return "";
}
但 exe 只是默默终止或在其他情况下返回“蓝色”。如果控制达到 __builtin_unreachable(); 的程度,真正会发生什么?是? Windows 上是否会出现一些错误消息框或者什么?有人能解释一下吗?
您告诉编译器该代码无法访问。如果你违背了这个承诺,你可能会遇到未定义的行为。这已记录在here:
如果控制流到达
点,则程序未定义。__builtin_unreachable