2

Under g++ 7.4 I could declare and initialize multiple variables like so:

std::pair<T, T> foo();

int main ()
{  auto [a, b] = foo();  }

However under Apple clang-703 it gives me an error.

I could not find any information about support for this feature in both compilers. Is it supported by modern compilers or should I not use it? Also what is the name of the feature?

Edit: The error is:

auto [a, b] = foo();
      ^1    ^2

1 => use of undeclared identifier 'a'.

2 => expected unqualified-id.

1 Answers1

1

This is a C++17 feature called a structured binding.

Clang 5 supports them; in fact it did since 4, as do many other compilers.

Trouble is, you're using Apple Clang, which has different version numbers. Possibly your compiler is just too old, or you did not enable C++17 functionality the way you did with your GCC 7.4 (in which C++17 was still experimental and opt-in).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055