BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdlb_pairutil

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.

Example 1: Basic Usage

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:

/// Load into the specified `key` and the specified `value` the key and
/// value for the first entry in the specified `map` and return `true`,
/// or else fail by storing 0 and an empty string and return `false`
/// when `map` is empty.
bool getFirst(int *key,
bsl::string *value,
{
if (map.empty()) {
*key = 0;
value->clear();
return false; // RETURN
}
bdlb::PairUtil::tie(*key, *value) = *map.begin();
return true;
}
void usageExample()
{
map[30782530] = "bbi10";
int uuid;
bsl::string username;
bool result = getFirst(&uuid, &username, map);
assert(result);
assert(30782530 == uuid);
assert("bbi10" == username);
}
Definition bslstl_string.h:1281
void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:5430
Definition bslstl_map.h:619
bool empty() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_map.h:3509
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_map.h:2751
static bsl::pair< t_FIRST &, t_SECOND & > tie(t_FIRST &first, t_SECOND &second)
Definition bdlb_pairutil.h:136