Back
Close

7 Features of C++17 that will simplify your code

fenbf
585.6K views

Structured Bindings

Do you often work with tuples?

If not, then you probably should start looking at it. Not only are tuples suggested for returning multiple values from a function, but they've also got special language support - so that the code is even easier and cleaner.

For example (got it from std::tie at cppreference):

std::set<S> mySet;
 
S value{42, "Test", 3.14};
std::set<S>::iterator iter;
bool inserted;
 
// unpacks the return val of insert into iter and inserted
std::tie(iter, inserted) = mySet.insert(value);

if (inserted)
    std::cout << "Value was inserted\n";

Notice that you need to declare iter and inserted first. Then you can use std::tie to make the magic... Still, it's a bit of code.

With C++17:

std::set<S> mySet;
 
S value{42, "Test", 3.14};

auto [iter, inserted] = mySet.insert(value);

One line instead of three! It's also easier to read and safer, isn't it?

Here's the full working code:

std::tie example

Also, you can now use const and write const auto [iter, inserted] and be const correct.

Structured Binding is not only limited to tuples, we have three cases:

1. If initializer is an array:

// works with arrays:
double myArray[3] = { 1.0, 2.0, 3.0 };  
auto [a, b, c] = myArray;

2. if initializer supports std::tuple_size<> and provides get<N>() function (the most common case I think):

auto [a, b] = myPair; // binds myPair.first/second

In other words, you can provide support for your classes, assuming you add get<N> interface implementation.

3. if initializer's type contains only non static, public members:

struct S { int x1 : 2; volatile double y1; };
S f();
const auto [ x, y ] = f();

Now it's also quite easy to get a reference to a tuple member:

auto& [ refA, refB, refC, refD ] = myTuple;

And one of the coolest usage (support to for loops!):

std::map myMap;    
for (const auto & [k,v] : myMap) 
{  
    // k - key
    // v - value
} 

Can you help me with the code below and make it working?

iteration over map, finish the code

BTW: Structured Bindings or Decomposition Declaration?

For this feature, you might have seen another name "decomposition declaration" in use. As I see this, those two names were considered, but now the standard (the draft) sticks with "Structured Bindings."

More Details in:

Working in GCC: 7.0, Clang: 4.0, MSVC: in VS 2017.3.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io