View Template Library


reference_pair<T1, T2>

Category: utilities

Component type: type

Description

Reference_pair<T1,T2> is a heterogeneous pair: it holds one object of type T1 and one of type T2. A reference_pair is much like a Container, in that it "owns" its elements. It is not actually a model of Container, though, because it does not support the standard methods (such as iterators) for accessing the elements of a Container. Reference_pair differs from pair by that the constructor is guarenteeded to use a reference for the constructor arguments without trying to make a reference to a reference which is illegal code. In other words reference_pair<&T1,&T2> will make a correct constructor, whereas pair will not.

Views that need to return two values often return a reference_pair.

Example

reference_pair<bool, double> result = do_a_calculation();
if (result.first)
  do_something_more(result.second);
else
  report_error();

Definition

Defined in reference_pair.h

Template parameters

Parameter

Description

Default

T1

The type of the first element stored in the reference_pair


T2

The type of the second element stored in the reference_pair


Model of

Assignable

Type requirements

T1 and T2 must both be models of Assignable. Additional operations have additional requirements. Reference_pair's default constructor may only be used if both T1 and T2 are DefaultConstructible, operator==() may only be used if both T1 and T2 are EqualityComparable, and operator<() may only be used if both T1 and T2 are LessThanComparable.

Public base classes

None.

Members

Member

Where defined

Description

first_type

reference_pair

See below.

second_type

reference_pair

See below.

reference_pair()

reference_pair

The default constructor. See below.

reference_pair(const first_type&, const second_type&)

reference_pair

The pair constructor. See below.

reference_pair(const pair&)

Assignable

The copy constructor

reference_pair& operator=(const reference_pair&)

Assignable

The assignment operator

first

reference_pair

See below.

second

reference_pair

See below.

bool operator==(const reference_pair&, const reference_pair&)

reference_pair

See below.

bool operator<(const reference_pair&, const reference_pair&)

reference_pair

See below.

template <class T1, class T2>
pair<T1, T2> make_reference_pair(const T1&, const T2&)

reference_pair

See below.

New members

These members are not defined in the Assignable requirements, but are specific to reference_pair.

Member

Description

first_type

The type of the pair's first component. This is a typedef for the template parameter T1

second_type

The type of the pair's second component. This is a typedef for the template parameter T2

reference_pair()

The default constructor. It uses constructs objects of types T1 and T2 using their default constructors. This constructor may only be used if both T1 and T2 are DefaultConstructible.

reference_pair(const first_type& x, const second_type& y)

The pair constructor. Constructs a pair such that first is constructed from x and second is constructed from y.

first

Public member variable of type first_type: the first object stored in the pair.

second

Public member variable of type second_type: The second object stored in the pair.

template <class T1, class T2, class T3, class T4>
bool operator==(const reference_pair<T1,T2>& x, 
                const reference_pair<T3,T4>& y);

The equality operator. The return value is true if and only the first elements of x and y are equal, and the second elements of x and y are equal. This operator may only be used if both T1, T2, T3, and T4 are EqualityComparable. This is a global function, not a member function.

template <class T1, class T2, class T3, class T4>
bool operator<(const reference_pair<T1,T2>& x, 
               const reference_pair<T3,T4>& y);

The comparison operator. It uses lexicographic comparison: the return value is true if the first element of x is less than the first element of y, and false if the first element of y is less than the first element of x. If neither of these is the case, then operator< returns the result of comparing the second elements of x and y. This operator may only be used if both T1, T2, T3, and T4 are LessThanComparable. This is a global function, not a member function.

template <class T1, class T2>
reference_pair<T1, T2> make_reference_pair(const T1& x, const T2& x)

Equivalent to refereenc_pair<T1, T2>(x, y). This is a global function, not a member function. It exists only for the sake of convenience.

Notes

See also

Assignable, Default Constructible, LessThan Comparable



VTL Home
Copyright © 1999 Konrad-Zuse-Zentrum für Informationstechnik Berlin & Gary Powell All Rights Reserved.