- Returning multiple Values from function C++ 11 vs C++ 17
- Returning compound objects
- Iterating over a compound collection
- Direct initialization
Returning multiple Values from function C++ 11 vs C++ 17 :
C++ 11 (std::tie):
std::tuple
{
char a = 'a';
int i = 123;
bool b = true;
return std::make_tuple(a, i, b); // packing variable into tuple
}
To access return value using C++ 11, we would need something like:
char a;
int i;
bool b;
std::tie(a, i, b) = mytuple(); // unpacking tuple into variables
Where the variables have to be defined before use and the types known in advance.
C++ 17 :
auto [a, i, b] = mytuple();
Returning compound objects :
This is the easy way to assign the individual parts of a compound type (such as a struct, pair etc) to different variables all in one go – and have the correct types automatically assigned. So let’s have a look at an example. If we insert into a map, then the result is a std::pair:
C++ 11 :
std::map mymap;
auto mapret = mymap.insert(std::pair('a', 100));
C++ 17 :
auto [itelem, success] = mymap.insert(std::pair(’a’, 100));
If (!success) {
// Insert failure
}
Iterating over a compound collection:
Structured Bindings also work with range-for as well. So considering the previous mymap definition, prior to C++17 we would iterate it with code looking like this:
C++ 11 :
for (const auto& entry : mymap) {
auto& key = entry.first;
auto& value = entry.second;
// Process entry
}
C++ 17 :
for (const auto&[key, value] : mymap) {
// Process entry using key and value
}
Direct initialization :
But as Structured Bindings can initialize from a tuple, pair etc, can we do direct initialization this way?
Yes we can. Consider:
auto a = ‘a’;
auto i = 123;
auto b = true;
which defines variables a as type char with initial value ‘a’, i as type int with initial value 123 and b as type bool with initial value true.
Using Structured Bindings, this can be written as:
C++ 17 :
auto [a, i, b] = tuple(‘a’, 123, true); // With no types needed for the tuple!
This will define the variables a, i, b the same as if the separate defines above had been used.
To access return value using C++ 11, we would need something like:
char a;
int i;
bool b;
std::tie(a, i, b) = mytuple(); // unpacking tuple into variables
Where the variables have to be defined before use and the types known in advance.
C++ 17 :
auto [a, i, b] = mytuple();
Returning compound objects :
This is the easy way to assign the individual parts of a compound type (such as a struct, pair etc) to different variables all in one go – and have the correct types automatically assigned. So let’s have a look at an example. If we insert into a map, then the result is a std::pair:
C++ 11 :
std::map
auto mapret = mymap.insert(std::pair('a', 100));
C++ 17 :
auto [itelem, success] = mymap.insert(std::pair(’a’, 100));
If (!success) {
// Insert failure
}
Iterating over a compound collection:
Structured Bindings also work with range-for as well. So considering the previous mymap definition, prior to C++17 we would iterate it with code looking like this:
C++ 11 :
for (const auto& entry : mymap) {
auto& key = entry.first;
auto& value = entry.second;
// Process entry
}
C++ 17 :
for (const auto&[key, value] : mymap) {
// Process entry using key and value
}
Direct initialization :
But as Structured Bindings can initialize from a tuple, pair etc, can we do direct initialization this way?
Yes we can. Consider:
auto a = ‘a’;
auto i = 123;
auto b = true;
which defines variables a as type char with initial value ‘a’, i as type int with initial value 123 and b as type bool with initial value true.
Using Structured Bindings, this can be written as:
C++ 17 :
auto [a, i, b] = tuple(‘a’, 123, true); // With no types needed for the tuple!
This will define the variables a, i, b the same as if the separate defines above had been used.
Comments
Post a Comment