Boost.Hana: boost::hana::pair< First, Second > Struct Template Reference

| Boost.Hana 1.0.0 Your standard library for metaprogramming |

Synopsis of associated functions | Friends | Public Member Functions | List of all members
boost::hana::pair< First, Second > Struct Template Reference
Data types

## Description

### template<typename First, typename Second>
struct boost::hana::pair< First, Second >

Generic container for two elements.

`hana::pair` is conceptually the same as `std::pair`. However, `hana::pair` automatically compresses the storage of empty types, and as a result it does not have the `.first` and `.second` members. Instead, one must use the `hana::first` and `hana::second` free functions to access the elements of a pair.

## Modeled concepts

1. `Comparable`
   Two pairs `(x, y)` and `(x', y')`are equal if and only if both` x == x'`and`y == y'`.
   // Copyright Louis Dionne 2013-2016
   // Distributed under the Boost Software License, Version 1.0.
   // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
   #include <boost/hana/equal.hpp>
   #include <boost/hana/not_equal.hpp>
   #include <boost/hana/pair.hpp>
   namespace hana = boost::hana;
   static_assert(hana::make_pair(1, 'x') == hana::make_pair(1, 'x'), "");
   static_assert(hana::make_pair(2, 'x') != hana::make_pair(1, 'x'), "");
   static_assert(hana::make_pair(1, 'y') != hana::make_pair(1, 'x'), "");
   int main() { }
2. `Orderable`
   Pairs are ordered as-if they were 2-element tuples, using a lexicographical ordering.
   // Copyright Louis Dionne 2013-2016
   // Distributed under the Boost Software License, Version 1.0.
   // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
   #include <boost/hana/less.hpp>
   #include <boost/hana/pair.hpp>
   namespace hana = boost::hana;
   static_assert(hana::make_pair(1, 'x') < hana::make_pair(1, 'y'), "");
   static_assert(hana::make_pair(1, 'x') < hana::make_pair(10, 'x'), "");
   static_assert(hana::make_pair(1, 'y') < hana::make_pair(10, 'x'), "");
   int main() { }
3. `Foldable`
   Folding a pair is equivalent to folding a 2-element tuple. In other words:
   fold_left(make_pair(x, y), s, f) == f(f(s, x), y)
   fold_right(make_pair(x, y), s, f) == f(x, f(y, s))
   Example:
   // Copyright Louis Dionne 2013-2016
   // Distributed under the Boost Software License, Version 1.0.
   // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
   #include <boost/hana/fold_left.hpp>
   #include <boost/hana/fold_right.hpp>
   #include <boost/hana/minus.hpp>
   #include <boost/hana/pair.hpp>
   #include <boost/hana/plus.hpp>
   namespace hana = boost::hana;
   static_assert(hana::fold_left(hana::make_pair(1, 3), 0, hana::plus) == 4, "");
   static_assert(hana::fold_right(hana::make_pair(1, 3), 0, hana::minus) == -2, "");
   int main() { }
4. `Product`
   The model of `Product` is the simplest one possible; the first element of a pair `(x, y)` is `x`, and its second element is `y`.
   // Copyright Louis Dionne 2013-2016
   // Distributed under the Boost Software License, Version 1.0.
   // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
   #include <boost/hana/first.hpp>
   #include <boost/hana/pair.hpp>
   #include <boost/hana/second.hpp>
   namespace hana = boost::hana;
   static_assert(hana::first(hana::make_pair(1, 'x')) == 1, "");
   static_assert(hana::second(hana::make_pair(1, 'x')) == 'x', "");
   int main() { }

## Synopsis of associated functions

template<>
constexpr auto | make< pair_tag >
| Creates a `hana::pair` with the given elements. More...
constexpr auto | make_pair = make<pair_tag>
| Alias to `make<pair_tag>`; provided for convenience. More...

## Friends

template<typename X , typename Y >
constexpr auto | operator== (X &&x, Y &&y)
| Equivalent to `hana::equal`
template<typename X , typename Y >
constexpr auto | operator!= (X &&x, Y &&y)
| Equivalent to `hana::not_equal`
template<typename X , typename Y >
constexpr auto | operator< (X &&x, Y &&y)
| Equivalent to `hana::less`
template<typename X , typename Y >
constexpr auto | operator> (X &&x, Y &&y)
| Equivalent to `hana::greater`
template<typename X , typename Y >
constexpr auto | operator<= (X &&x, Y &&y)
| Equivalent to `hana::less_equal`
template<typename X , typename Y >
constexpr auto | operator>= (X &&x, Y &&y)
| Equivalent to `hana::greater_equal`

## Public Member Functions

constexpr | pair ()
| Default constructs the `pair`. Only exists when both elements of the pair are default constructible.
constexpr | pair (First const &first, Second const &second)
| Initialize each element of the pair with the corresponding element. Only exists when both elements of the pair are copy-constructible.
template<typename T , typename U >
constexpr | pair (T &&t, U &&u)
| Initialize both elements of the pair by perfect-forwarding the corresponding argument. Only exists when both arguments are implicitly-convertible to the corresponding element of the pair.
template<typename T , typename U >
constexpr | pair (pair< T, U > const &other)
| Copy-initialize a pair from another pair. Only exists when both elements of the source pair are implicitly convertible to the corresponding element of the constructed pair.
template<typename T , typename U >
constexpr | pair (pair< T, U > &&other)
| Move-initialize a pair from another pair. Only exists when both elements of the source pair are implicitly convertible to the corresponding element of the constructed pair.
template<typename T , typename U >
constexpr pair & | operator= (pair< T, U > const &other)
| Assign a pair to another pair. Only exists when both elements of the destination pair are assignable from the corresponding element in the source pair.
template<typename T , typename U >
constexpr pair & | operator= (pair< T, U > &&other)
| Move-assign a pair to another pair. Only exists when both elements of the destination pair are move-assignable from the corresponding element in the source pair.

## Associated functions

template<typename First, typename Second>
template<>

constexpr auto make< pair_tag >

related

Initial value:
= [](auto&& first, auto&& second)
-> hana::pair<std::decay_t<decltype(first)>, std::decay_t<decltype(second)>>
{
return {forwarded(first), forwarded(second)};
}

Creates a `hana::pair` with the given elements.

## Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#include <boost/hana/core/make.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/first.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/second.hpp>
namespace hana = boost::hana;
static_assert(hana::first(hana::make<hana::pair_tag>(1, 'x')) == 1, "");
static_assert(hana::second(hana::make<hana::pair_tag>(1, 'x')) == 'x', "");
static_assert(hana::make_pair(1, 'x') == hana::make<hana::pair_tag>(1, 'x'), "");
int main() { }
template<typename First, typename Second>

constexpr auto make_pair = make<pair_tag>

related

Alias to `make<pair_tag>`; provided for convenience.

## Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
#include <boost/hana/core/make.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/first.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/second.hpp>
namespace hana = boost::hana;
static_assert(hana::first(hana::make<hana::pair_tag>(1, 'x')) == 1, "");
static_assert(hana::second(hana::make<hana::pair_tag>(1, 'x')) == 'x', "");
static_assert(hana::make_pair(1, 'x') == hana::make<hana::pair_tag>(1, 'x'), "");
int main() { }