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(从未到达),因为它使代码更难阅读.
使用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_invalid为noreturn将告诉编译器,foo只要采用具有该功能的代码路径,它也将不返回throw_invalid.
| 归档时间: |
|
| 查看次数: |
167 次 |
| 最近记录: |