|
BDE 4.39.x Production Release
|
Provide support functions for bsl::pair.
Provide support functions for bsl::pair.
bsl::pairThis component provides the class bdlb::PairUtil, which has the following tools for working with bsl::pair:
tie: function that is intended to be used in place of bsl::tie when the right-hand side of the assignment is a bsl::pairadaptForRanges: function that adapt bsl containers whose value type is a bsl::pair type to work with range adaptorsstdPairRefAdaptor: function object that takes a bsl::pair by reference and returns a std::pair where each element is a reference to the corresponding member of the bsl::pairThis section illustrates intended use of this component.
Suppose we need to implement a function that takes a bsl::map and stores into out-parameters the key and value corresponding to the first entry in the map. Using bsl::maps container interface, we can obtain a reference to a bsl::pair of the key and value. We can then use bdlb::PairUtil::tie to assign from both the key and value in a single expression:
Let's assume that we have a bsl::map storing employee indexes and their names, and we want to get a list of employee names:
However, if we were to try and access the names using bsl::views::values we would see a compilation error:
auto names = employees | bsl::views::values; // does not compile auto namesIt = names.begin(); assert("John Dow" == *namesIt);
This fails to because bsl::pair, unlike the std::pair, does not model the tuple-like concept, which is a requirement of the bsl::views::values. This problem can be resolved using the bdlb::PairUtil::adaptForRanges function on the container:
And of course this function allows you to create chains of adaptors using a pipeline operator: