Provide a function that creates a pair of references.
More...
Namespaces |
namespace | bdlb |
Detailed Description
- Outline
-
-
- Purpose:
- Provide a function that creates a pair of references.
-
- Classes:
-
-
- Description:
- This component provides the class
bdlb::PairUtil
, which has a single static member function, tie
, which is intended to be used in place of bsl::tie
when the right-hand side of the assignment is a bsl::pair
.
-
- Usage:
- This 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::map
s 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: bool getFirst(int *key,
bsl::string *value,
const bsl::map<int, bsl::string>& map)
{
if (map.empty()) {
*key = 0;
value->clear();
return false;
}
bdlb::PairUtil::tie(*key, *value) = *map.begin();
return true;
}
void usageExample()
{
bsl::map<int, bsl::string> map;
map[30782530] = "bbi10";
int uuid;
bsl::string username;
bool result = getFirst(&uuid, &username, map);
assert(result);
assert(30782530 == uuid);
assert("bbi10" == username);
}