【问题标题】:Can a constexpr function contain a label?constexpr 函数可以包含标签吗?
【发布时间】:2020-11-16 04:18:34
【问题描述】:

这个program

constexpr void f() { x: ; }

是gcc编译的,但是clang说:

error: statement not allowed in constexpr function

那么这段代码有效吗?

【问题讨论】:

  • cppreference 表示constexpr 函数不能包含带有casedefault 以外的标签的语句。看起来来自标准的第 10.1.5 节 dcl.constexpr。

标签: c++ language-lawyer constexpr


【解决方案1】:

正如comment by Nathan Pierson 中指出的那样,Clang 是正确的,并且代码格式不正确。根据目前的工作草案(包括 C++20),dcl.constexpr#3 说:

constexpr 函数的定义应满足以下要求:

...

它的函数体不应包含

...

一个标识符标签,

...

...

【讨论】:

    猜你喜欢
    • 2015-12-26
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 2013-06-14
    • 2011-01-10
    • 1970-01-01
    • 2011-08-31
    相关资源
    最近更新 更多