Boost.Hana  1.7.1
Your standard library for metaprogramming
boost::hana::pair< First, Second > Struct Template Reference

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.

Note
When you use a container, remember not to make assumptions about its representation, unless the documentation gives you those guarantees. More details in the tutorial.

Modeled concepts

  1. Comparable
    Two pairs (x, y) and ‘(x’, y')are equal if and only if both x == x'andy == y'`.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    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() { }
    Defines boost::hana::equal.
    Namespace containing everything in the library.
    Definition: accessors.hpp:20
    Defines boost::hana::not_equal.
    Defines boost::hana::pair.
  2. Orderable
    Pairs are ordered as-if they were 2-element tuples, using a lexicographical ordering.
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    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() { }
    Defines boost::hana::less.
  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))
    constexpr auto make_pair
    Alias to make<pair_tag>; provided for convenience.
    Definition: pair.hpp:155
    Example:
    // Copyright Louis Dionne 2013-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    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() { }
    Defines boost::hana::fold_left.
    Defines boost::hana::fold_right.
    constexpr auto minus
    Subtract two elements of a group.
    Definition: minus.hpp:51
    constexpr auto plus
    Associative binary operation on a Monoid.
    Definition: plus.hpp:47
    Defines boost::hana::minus.
    Defines boost::hana::plus.
  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-2017
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    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() { }
    Defines boost::hana::first.
    constexpr auto first
    Returns the first element of a pair.
    Definition: first.hpp:33
    constexpr auto second
    Returns the second element of a pair.
    Definition: second.hpp:32
    Defines boost::hana::second.

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 friend auto operator== (X &&x, Y &&y)
 Equivalent to hana::equal
 
template<typename X , typename Y >
constexpr friend auto operator!= (X &&x, Y &&y)
 Equivalent to hana::not_equal
 
template<typename X , typename Y >
constexpr friend auto operator< (X &&x, Y &&y)
 Equivalent to hana::less
 
template<typename X , typename Y >
constexpr friend auto operator> (X &&x, Y &&y)
 Equivalent to hana::greater
 
template<typename X , typename Y >
constexpr friend auto operator<= (X &&x, Y &&y)
 Equivalent to hana::less_equal
 
template<typename X , typename Y >
constexpr friend 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 pairoperator= (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 pairoperator= (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

◆ make< pair_tag >

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-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
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() { }
Defines boost::hana::make.

◆ make_pair

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-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
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() { }