我知道除其他事项外(如 auto_ptr),异常规范已在 C++11 中弃用。
是否有任何方法可以使用以下代码从 g++ 4.8 获取警告?
struct F{
void foo() throw (int){}
};
int main()
{
}
我已经尝试过 -Wall -pedantic -Wextra -Wdeprecated-declarations,但没有成功。
最佳答案
你可以使用
class __attribute__((deprecated)) old_style_throw_specification;
class old_style_throw_specification {};
#define throw(...) throw(old_style_throw_specification, __VA_ARGS__)
或者如果你需要支持空抛规范(感谢@John5342 指出这一点),你可以使用
#define throw(...) throw(old_style_throw_specification, ##__VA_ARGS__)
对于宏,但您需要使用 GNU 扩展进行编译:-std=gnu++11
因为上面的 C++11 并不是严格合法的。
关于c++ - 如何获得有关旧异常规范的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22942734/