在函数中使用throw关键字会产生带有gcc的警告

mal*_*lat 3 c++ portability gcc throw

我需要找到一种方法(请使用C++ 03,不能使用C++ 11)来删除gcc对以下(伪)代码产生的警告:

#include <stdexcept>

void throw_invalid()
{
  throw std::invalid_argument( "invalid" );
}

int foo(const char * str)
{
  if( str ) return 42;
  throw_invalid();
  // insert portable -fake- code that will get optimized away
}
Run Code Online (Sandbox Code Playgroud)

我的代码需要至少在gcc 5.x(-Wall)和Visual Studio上免费警告.我的throw_invalid功能是避免锅炉板代码,并将异常集中在与无效参数相关的单个函数中.

目前的警告是:

$ g++ -Wall -c foo.cxx 
b.cxx: In function ‘int foo(const char*)’:
b.cxx:13:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
Run Code Online (Sandbox Code Playgroud)

我想避免添加假return -1(从未到达),因为它使代码更难阅读.

mka*_*aes 6

使用c ++ 11,您可以使用属性说明符 [[noreturn]].

像这样:

[[noreturn]] void throw_invalid()
{
     throw std::invalid_argument( "invalid" );
}

int foo(const char * str)
{
    if( str ) return 42;
    throw_invalid();
    // insert portable -fake- code that will get optimized away
}
Run Code Online (Sandbox Code Playgroud)

更新

就像Walter在评论中提到的那样,即使函数foo是非void函数和触发错误的函数,它也是throw_invalid需要属性的函数.设置throw_invalidnoreturn将告诉编译器,foo只要采用具有该功能的代码路径,它也将不返回throw_invalid.

  • @Default非void函数是`foo()`,编译器发出警告,因为它没有返回值.但是,永远无法达到此函数的结尾,因为`throw_invalid()`永远不会返回.这个答案中提供的解决方案告诉编译器这个事实,这样就可以避免虚假警告. (2认同)