sus*_*att 6 c++ warnings g++ visual-studio-2010
说我有以下功能:
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,但我也很好奇其他人),为了这个检查的目的,要忽略某个控制路径,而不是完全抑制警告或返回一个无意义的假人功能结束时的值?
Mat*_* M. 10
你可以注释函数fatalError
(它的声明)让编译器知道它永远不会返回.
在C++ 11中,这将是这样的:
[[noreturn]] void fatalError(std::string const&);
Run Code Online (Sandbox Code Playgroud)
在C++ 11之前,您有编译器特定的属性,例如GCC:
void fatalError(std::string const&) __attribute__((noreturn));
Run Code Online (Sandbox Code Playgroud)
或Visual Studio的:
__declspec(noreturn) void fatalError(std::string const&);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
838 次 |
最近记录: |