Partager via


make_pair

A template function that you can use to construct objects of type pair, where the component types are automatically chosen based on the data types that are passed as parameters.

template<class Type1, class Type2> 
  pair<Type1, Type2> make_pair(Type1& Val1, Type2& Val2);
template<class Type1, class Type2> 
  pair<Type1, Type2> make_pair(Type1& Val1, Type2&& Val2); 
template<class Type1, class Type2> 
  pair<Type1, Type2> make_pair(Type1&& Val1, Type2& Val2); 
template<class Type1, class Type2> 
  pair<Type1, Type2> make_pair(Type1&& Val1, Type2&& Val2);

Parameters

  • Val1
    Value that initializes the first element of pair.

  • Val2
    Value that initializes the second element of pair.

Return Value

The pair object that's constructed: pair<Type1, Type2>(Val1, Val2).

Remarks

make_pair converts object of type reference_wrapper Class to reference types and converts decaying arrays and functions to pointers.

In the returned pair object, Type1 is determined as follows:

  • If the input type Type1 is reference_wrapper<X>, the returned type Type1 is X&.

  • Otherwise, the returned type Type1 is decay<Type1>::type. If decay Class is not supported, the returned type Type1 is the same as the input type Type1.

The returned type Type2 is similarly determined from the input type Type2.

One advantage of make_pair is that the types of objects that are being stored are determined automatically by the compiler and do not have to be explicitly specified. Don't use explicit template arguments such as make_pair<int, int>(1, 2) when you use make_pair because it is unnecessarily verbose and adds complex rvalue reference problems that might cause compilation failure. For this example, the correct syntax would be make_pair(1, 2)

The make_pair helper function also makes it possible to pass two values to a function that requires a pair as an input parameter.

Example

For an example about how to use the helper function make_pair to declare and initialize a pair, see pair Structure.

Requirements

Header: <utility>

Namespace: std

See Also

Reference

<utility>

Lvalues and Rvalues