【发布时间】:2015-12-03 12:44:15
【问题描述】:
我需要找到一种方法(请使用 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
}
我的代码至少需要在 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]
}
^
我想避免添加虚假的return -1(从未达到),因为它会使代码更难阅读。
【问题讨论】:
标签: c++ gcc portability throw