由于noexcept说明符,尾随返回类型语法失败

Trailing return-type syntax fails with noexcept specifier?

本文关键字:返回类型 语法 失败 noexcept 说明符 由于      更新时间:2023-10-16

代码正常运行:

void f() noexcept {}

但是下面的命令在GCC 4.7.2中出错:

auto f() -> void noexcept {}
// error: expected initializer before ‘noexcept’

我读过的文章没有说过不能在训练返回类型中指定noexcept。这是一个bug吗(在最新版本的GCC中已经修复了吗)?或者这是标准明确禁止的吗?

语法不正确。应该是:

auto f() noexcept -> void { }

c++ 11标准第8.4.1/2段:

D1 (参数声明子句)cv-qualifier-seq(opt)

ref-qualifier(opt) *exception-specification(opt)* attribute-specifier-seq(opt) *trailing-return-type(opt)*

如8.3.5所述。函数只能在命名空间或类作用域中定义。

相关文章: