Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdlb_pairutil
[Package bdlb]

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:
bdlb::PairUtil namespace for the bdlb::PairUtil::tie function
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::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:
  bool getFirst(int                              *key,
                bsl::string                      *value,
                const bsl::map<int, bsl::string>& map)
      // 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.
  {
      if (map.empty()) {
          *key = 0;
          value->clear();
          return false;                                             // RETURN
      }
      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);
  }