正常
Structured Bindings (C++17)
In a Nutshell
A single line of syntax that destructures the elements of a tuple, pair, struct, or array into independent variables at once, sparing you std::get and field-by-field access.
Header
None (language feature)
Core API Quick Reference
| Binding Form | Syntax | Description |
|---|---|---|
| Bind by value | auto [a, b] = expr; | Copies the elements into new variables |
| Lvalue reference | auto& [a, b] = expr; | Binds references to the original object |
| Read-only reference | const auto& [a, b] = expr; | Const reference, avoids copying |
| Forwarding reference | auto&& [a, b] = expr; | Perfect forwarding semantics |
| Array destructuring | auto [a, b, c] = arr; | Binds to array elements (count must match) |
| Pair destructuring | auto [key, val] = *map_iter; | Binds to a pair's first/second |
| Tuple destructuring | auto [x, y, z] = tup; | Binds to get<I> of tuple-like types |
| Struct destructuring | auto [x, y] = point; | Binds to public data members (in declaration order) |
Minimal Example
Expand codeCollapse23 lines
C++
// Standard: C++17
#include <iostream>
#include <map>
#include <tuple>
struct Point { double x, y; };
int main() {
// Struct destructuring
Point p{1.0, 2.0};
auto [px, py] = p;
std::cout << px << ", " << py << "\n"; // 1, 2
// Pair destructuring (map iteration)
std::map<int, const char*> m{{1, "one"}, {2, "two"}};
for (const auto& [key, val] : m) {
std::cout << key << ": " << val << "\n";
}
// Tuple destructuring
auto [a, b, c] = std::make_tuple(10, 20, 30);
std::cout << a + b + c << "\n"; // 60
}Embedded Applicability: High
- Pure compile-time syntactic sugar with zero runtime overhead; the generated code is fully equivalent to manual field access
- Simplifies unpacking multi-field structures such as register banks and sensor data, improving readability
- Combine with
const auto&to avoid copies — ideal for read-only access to hardware-mapped structs - Well supported in mainstream embedded toolchains (GCC 7+, ARM Clang 6+) since C++17
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| 7 | 4.0 | 19.11 |
See Also
Some content referenced from cppreference.com, licensed under CC-BY-SA 4.0