相关疑难解决方法(0)

我可以告诉编译器考虑关于返回值关闭的控制路径吗?

说我有以下功能:

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.
}
Run Code Online (Sandbox Code Playgroud)

编译器通常会抱怨这一点,并说"并非所有控制路径都返回一个值".这在技术上是真实的,但没有返回值的控制路径终止程序函数结束之前,因此是语义正确的.有没有办法告诉编译器(在我的情况下VS2010,但我也很好奇其他人),为了这个检查的目的,要忽略某个控制路径,而不是完全抑制警告或返回一个无意义的假人功能结束时的值?

c++ warnings g++ visual-studio-2010

6
推荐指数
1
解决办法
838
查看次数

标签 统计

c++ ×1

g++ ×1

visual-studio-2010 ×1

warnings ×1