假设我有以下功能:
Thingy& getThingy(int id)
{
for ( int i = 0; i < something(); ++i )
{
// normal execution guarantees that the Thingy we're looking for exists
if ( thingyArray[i].id == id )
return thingyArray[i];
}
// If we got this far, then something went horribly wrong and we can't recover.
// This function terminates the program.
fatalError("The sky is falling!");
// Execution will never reach this point.
}
编译器通常会对此抱怨,说“并非所有控制路径都返回值”。这在技术上是正确的,但是不返回值的控制路径会在函数结束之前中止程序,因此在语义上是正确的。有没有办法告诉编译器(在我的情况下是 VS2010,但我也对其他人感到好奇)为了进行此检查而忽略某个控制路径,而不会完全抑制警告或返回无意义的假人函数末尾的值?