constexpr if with initializer 由标准保证吗? 'constexpr(constexpr auto x = f(); x) { }'
Is constexpr if with initializer guaranteed by the standard? 'constexpr(constexpr auto x = f(); x) { }'
我找不到任何关于新 C++17 if 初始化语法的信息
和 '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
with initializer 是否由标准保证,因为 constexpr if
只是一个“if
with constexpr
”,或者它没有保证并且必须明确添加到标准?
Selection statements with initializer 提案提到 if constexpr
,并声明 "the facilities of if constexpr
work just as well with the extended if
statement from this proposal"。
N4606 [stmt.if]p3 中关于带有初始值设定项的 if
语句的规范明确允许使用 if constexpr
.
这是 N4606 [stmt.if]p3 所说的:
An if statement of the form
if constexpr[opt] ( init-statement condition ) statement
is equivalent to
{
init-statement
if constexpr[opt] ( condition ) statement
}
and an if statement of the form
if constexpr[opt] ( init-statement condition ) statement else statement
is equivalent to
{
init-statement
if constexpr[opt] ( condition ) statement else statement
}
except that names declared in the init-statement are in the same declarative region as those declared in the condition.
我找不到任何关于新 C++17 if 初始化语法的信息 和 '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
with initializer 是否由标准保证,因为 constexpr if
只是一个“if
with constexpr
”,或者它没有保证并且必须明确添加到标准?
Selection statements with initializer 提案提到 if constexpr
,并声明 "the facilities of if constexpr
work just as well with the extended if
statement from this proposal"。
N4606 [stmt.if]p3 中关于带有初始值设定项的 if
语句的规范明确允许使用 if constexpr
.
这是 N4606 [stmt.if]p3 所说的:
An if statement of the form
if constexpr[opt] ( init-statement condition ) statement
is equivalent to
{ init-statement if constexpr[opt] ( condition ) statement }
and an if statement of the form
if constexpr[opt] ( init-statement condition ) statement else statement
is equivalent to
{ init-statement if constexpr[opt] ( condition ) statement else statement }
except that names declared in the init-statement are in the same declarative region as those declared in the condition.