[C++] structured binding declaration
Updated:
개요
- rvalue의 하위 요소를 바인딩
예제
- 코드
#include <iostream> #include <string> #include <tuple> using namespace std; void test1() { int i[2] = {1, 3}; auto [x1, y1] = i; cout << x1 << ", " << y1 << endl; ++x1; cout << i[0] << ", " << i[1] << endl; cout << endl; auto &[x2, y2] = i; cout << x1 << ", " << y1 << endl; ++x2; cout << i[0] << ", " << i[1] << endl; } void test2() { tuple<int, string> t = make_tuple(1, "a"); cout << get<0>(t) << ", " << get<1>(t) << endl; ++get<0>(t); cout << get<0>(t) << ", " << get<1>(t) << endl; cout << endl; auto [i1, s1] = t; cout << i1 << ", " << s1 << endl; ++i1; cout << get<0>(t) << ", " << get<1>(t) << endl; cout << endl; auto &[i2, s2] = t; cout << i2 << ", " << s2 << endl; ++i2; cout << get<0>(t) << ", " << get<1>(t) << endl; } void test3() { struct S { int i; string s; }; auto [i, s] = S{1, "a"}; cout << i << ", " << s << endl; } int main() { test1(); cout << "------" << endl; test2(); cout << "------" << endl; test3(); return 0; }
- 실행 결과
1, 3 1, 3 2, 3 2, 3 ------ 1, a 2, a 2, a 2, a 2, a 3, a ------ 1, a