1

我知道除其他外(如 auto_ptr),异常规范在 C++11 中已被弃用。

有没有办法使用以下代码从 g++ 4.8 获得警告?

struct F{
    void foo() throw (int){}
};
int main()
{
}

我已经尝试过-Wall -pedantic -Wextra -Wdeprecated-declarations,但没有任何成功。

4

1 回答 1

3

您可以使用

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。

活生生的例子

于 2014-04-08T16:32:47.693 回答