Structured binding declaration 可以把對應的 tuple、pair、vector 展開,讓 code 更好讀。 ## 展開 tuple 假設我們有一個 tuple: ```cpp std::tuple person{ "John", 32, 170 }; auto& [name, age, tall] = person; ``` name 會是 "John" age 是 32 tall 是 170 但比較好用的時候還是用來展開 function 的回傳值,假設我們有一個 function 會回傳 tuple: ```cpp std::tuple getPersonData(uint32_t id) { return std::make_tuple("John", 32, 170); } auto [name, age, tall] = getPersonData(id); ``` ## 展開 array `std::vector` 也是一樣的用法: ```cpp #include std::array my_vec = { 5, 7, 10 }; auto& [num1, num2, num3] = my_vec; ``` 或是: ```cpp float rect[4]{ 5.0f, 6.0f, 120.0f, 200.0f }; auto& [x, y, w, h] = rect; ```