Quickly Write Efficient Code With Modern Structured Bindings Available In C++17 On Windows With C++Builder
C++17 has a new feature that consolidates syntactic sugar and automatic type deduction: structured bindings. This helps to assign values from tuples, pairs, and structs into individual variables. In another programming language, you can find this as unpacking.
Applying a structured binding to specify various variables from one bundled structure is one step. Structured bindings always applied with the same pattern:
auto [var1, var2, ...] = pair, tuple, struct, or array expression>; |
- The list of variables var1, var2 – must exactly match the number of variables contained by the expression being assigned from
- Then there should be one of the following
- std::pair
- std::tuple
- struct
- or array
- The type can be auto, const auto, or even auto&&
If you write too many or not enough variables between the square brackets, the compiler will give an error, telling us about our mistake
std::tuplefloat, int, long> tup {3.55, 1, 99}; auto [a, b] = tup; // gives an error |
Here is a complete real example of a use case for structured bindings:
#include
#include
#include
// Demonstrate structured bindings – here, a method might in the past
// have had a bool success result, and an error param written to.
// Now, can return both (all error info) as one tuple, bound automatically
// to local vars through the auto keyword (much nicer than std::tie
// in C++11/14)
auto connect_to_network() {
// The connection could succeed, or fail with an error message
// Here, mimic it failing
bool connected{ false }; // failed!
std::string str_error{ “404: resource not found” };
return std::make_tuple(connected, str_error);
}
int _tmain(int argc, _TCHAR* argv[]) {
auto [connected, str_error] = connect_to_network();
if (!connected) {
std::cout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#ifdef _WIN32 #include #else typedef char _TCHAR; #define _tmain main #endif
#include #include #include
// Demonstrate structured bindings – here, a method might in the past // have had a bool success result, and an error param written to. // Now, can return both (all error info) as one tuple, bound automatically // to local vars through the auto keyword (much nicer than std::tie // in C++11/14)
auto connect_to_network() { // The connection could succeed, or fail with an error message // Here, mimic it failing bool connected{ false }; // failed!
std::string str_error{ “404: resource not found” };
return std::make_tuple(connected, str_error); }
int _tmain(int argc, _TCHAR* argv[]) { auto [connected, str_error] = connect_to_network();
if (!connected) { std::cout str_error; }
system(“pause”);
return 0; } |
As you can see, by using structured bindings, you can write more efficient and clean code.
Be sure to check out the official documentation on structured bindings here!
Check out the structured bindings demo for C++Builder on GitHub!