如果标准保证使用初始值设定项,constexpr 是否? 'constexpr(constexpr auto x = f(); x) { }'

Is constexpr if with initializer guaranteed by the standard? 'constexpr(constexpr auto x = f(); x) { }'

本文关键字:constexpr 标准 auto 是否 如果      更新时间:2023-10-16

如果初始值设定项语法,我找不到任何关于新C++17的信息和中的"constexpr if"

http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0128r1.html

不过,Clang HEAD支持该语法。。。

constexpr auto f() { return true; }
int main() {
    if constexpr(constexpr auto x = f(); x) { }
}

在线代码在这里->http://melpon.org/wandbox/permlink/dj3a9ChvjhlNc8nr

带有初始化器的constexpr if是否由标准保证,因为constexpr if只是一个"带有constexprif",或者它没有保证,必须显式添加到标准中?

带有初始值设定项提案的Selection语句提到了if constexpr,并表示"if constexpr的功能与该提案中的扩展if语句同样有效"。

关于N4606[stmt.if]p3中带有初始值设定项的if语句的规范明确允许使用if constexpr

以下是N4606[stmt.if]p3所说的:

形式的if语句

if constexpr[opt] ( init-statement condition ) statement

相当于

{
  init-statement
  if constexpr[opt] ( condition ) statement
}

以及形式的if语句

if constexpr[opt] ( init-statement condition ) statement else statement

相当于

{
  init-statement
  if constexpr[opt] ( condition ) statement else statement
}

除了init语句中声明的名称与条件中声明的那些名称位于同一声明性区域之外。