Kietz
Kietz

Reputation: 1406

Why doesn't ISO c++17 permit a structured binding declaration in a condition?

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?

Upvotes: 3

Views: 658

Answers (1)

NathanOliver
NathanOliver

Reputation: 180415

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)

Upvotes: 6

Related Questions