Tuesday, October 17, 2017

C++17: Structured binding declarations

C++17 provides structured binding declarations, which let you declare multiple variables initialized from a tuple or a struct. Here is an example:

#include <iostream>

struct MyStruct
{
  int    mA;
  int    mB;
  double mC;
  MyStruct() : mA(1), mB(2), mC(3.3) {};
};

int main()
{
  MyStruct myStruct;

  auto [a, b, c] = myStruct;

  std::cout << a << " " << b << " " << c << std::endl;
  return 0;
}
// Output: 1 2 3.3
References:
https://en.wikipedia.org/wiki/C%2B%2B17
https://skebanga.github.io/structured-bindings/

No comments:

Post a Comment