Clang emits a warning if I use a structured binding declaration as a condition:
$ cat hello.cc
int main() {
struct A { int i; operator bool() { return true; } };
if (auto [i] = A{0}) {
return i;
}
return -1;
}
$ clang++-10 -std=c++17 hello.cc
hello.cc:3:12: warning: ISO C++17 does not permit structured binding declaration in a condition [-Wbinding-in-condition]
if (auto [i] = A{0}) {
^~~
1 warning generated.
I don't see this in dcl.struct.bind or stmt.select ; where would I see that this is forbidden?
Furthermore: what is the rationale behind forbidding this?
The grammar for the if statement is
if constexpr(opt) ( init-statement(opt) condition) statement
and as you can see condition
is required while the init-statement
is optional. That means in if (auto [i] = A{0})
that auto [i] = A{0}
is the condition, not the init-statment
. condition
is defined as
condition:
expression
attribute-specifier-seq(opt) decl-specifier-seq declarator brace-or-equal-initializer
and that does not allow for a structured binding as the grammar for that is
attribute-specifier-seq(opt) decl-specifier-seq ref-qualifier(opt) [ identifier-list ] initializer ;
Good news is you can get what you want by adding a condition to your if statement like
if (auto [i] = A{0}; i)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.