BDE 4.14.0 Production release
|
Macros | |
#define | BSLSTL_PAIR_NO_IMPLICIT_DELETED_FOR_MOVE_OPS 1 |
#define | BSLSTL_PAIR_DO_NOT_DEFAULT_THE_DEFAULT_CONSTRUCTOR 1 |
#define | BSLSTL_PAIR_DO_NOT_SFINAE_TEST_IS_SWAPPABLE 1 |
Provide a simple struct
with two members that may use allocators.
bslma::Allocator
Canonical header: bsl_utility.h
bsl::pair
is an allocator-aware version of std::pair
. The bsl::pair
class template is instantiated on two types, T1
and T2
, and provides two public data members, first
and second
of types T1
and T2
, respectively. Each data member might or might not allocate memory using bslma::Allocator
. Its interface is identical to std::pair
except that it has constructors that take optional allocator arguments to correctly construct the member variables. For example, a bsl::pair<std::string, int>
has member first
of type std::string
and second
of type int
. A client can pass a bslma::Allocator
pointer to the pair constructor and the constructor will pass it through to the first
member. Similarly, the copy constructor takes an optional bslma::Allocator
pointer and copy-constructs the first
member using that allocator.
bsl::pair
is unusual in that its data members, first
and second
, are public. Once constructed, a client program accesses these members directly. This part of the interface is identical to std::pair
, for which bsl::pair
is intended to be a drop-in replacement.
bsl::pair
has four constructors: a default constructor that default-constructs the two data members, a copy constructor that copy-constructs each data member, a constructor taking two arguments of type T1
and T2
, which are used to copy-construct first
and second
respectively, and a conversion constructor template for converting from a bsl::pair
of different types, PARAM_1
and PARAM_2
, provided PARAM_1
is convertible to T1
and PARAM_2
is convertible to T2
. Each constructor also has an optional bslma::Allocator
pointer argument. If neither T1
nor T2
use bslma::Allocator
, this argument is ignored. Otherwise, either first
or second
, or both, depending on whether each type uses bslma::Allocator
, will be passed the bslma::Allocator*
argument during construction. Whether or not a type uses bslma::Allocator
is determined by querying the bslma::UsesBslmaAllocator
trait for that type. This component also defines a full set of equality and relational operators that can be instantiated if T1
and T2
both provide those operators.
A bsl::pair
declares a set of associated type traits that are computed from the type traits of T1
and T2
. For each supported type trait, a given specialization of bsl::pair
has that trait if and only if both T1
and T2
have that trait. Supported traits are:
In addition, a bsl::pair
specialization has the bslma::UsesBslmaAllocator
trait if either T1
or T2
have that trait, or both.
Language standards after the first (C++98) and its corrigendum (C++03) have added code C++ language features, type traits as well as requirements that affect the exact interface pair
provides. This section describes such enhancements as they become available.
C++11 has introduced type traits, many of which needs compiler backing to be (automatically implementable) and also introduced "defaulted" (special member) functions that generate code without making mistakes. Before C++11 it was not possible to determine if a type had a default constructor in a non-intrusive manner. C++11 makes it possible using <type_traits> to determine that, and = default
makes it possible to create special member functions that exists only when they can be implemented properly.
Hence, when using compilers with a reasonably complete implementation of C++11 (notably MSVC 2013 is not one of those) we only implement the default constructor of pair if both types inside the pair type have a default constructor. Note that it means that when using C++11 (except in compilers not implementing it properly) a bsl::pair
that stores a reference type (such as int&
), or any other type that cannot be default constructed using the syntax T v{};
will cause pair to neither declare nor define a default constructor. So from C++11 onwards a type of bsl::pair<T1, T2>
will have a default constructor only if both types T1
and T2
are default constructible. Otherwise, pair will have a default constructor that gives a compile-error (only) if called.
A bsl::pair
is a very simple object when used without allocators. Our usage example concentrates on the use of allocators with bsl::pair
. First, we create a utility function that copies a null-terminated string into memory allocated from a supplied allocator:
We create a simple string class that holds strings allocated from a supplied allocator. It uses myStrDup
(above) in its implementation:
Our main program creates a mapping from strings to integers. Each node of the mapping consists of a bsl::pair<my_String, int>
. The program allocates memory from a test allocator in order to ensure that there are no leaks:
When constructing a Node
, an allocator is supplied in addition to parameters for the first
and second
data members.
Clean up at end.
#define BSLSTL_PAIR_DO_NOT_DEFAULT_THE_DEFAULT_CONSTRUCTOR 1 |
#define BSLSTL_PAIR_DO_NOT_SFINAE_TEST_IS_SWAPPABLE 1 |
#define BSLSTL_PAIR_NO_IMPLICIT_DELETED_FOR_MOVE_OPS 1 |