Skip to content

Commit 14ee44d

Browse files
committed
[pair] Support reference_wrappers in make_pair
1 parent 78f6e91 commit 14ee44d

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

include/boost/hana/fwd/pair.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ namespace boost { namespace hana {
105105
//! @relates hana::pair
106106
//!
107107
//!
108+
//! @note
109+
//! `make<pair_tag>` supports reference wrappers. When a reference wrapper
110+
//! is passed to it, the resulting `hana::pair` will hold a reference
111+
//! to the object enclosed in the reference wrapper instead of the object
112+
//! itself.
113+
//!
114+
//!
108115
//! Example
109116
//! -------
110117
//! @include example/pair/make.cpp

include/boost/hana/pair.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Distributed under the Boost Software License, Version 1.0.
1313
#include <boost/hana/fwd/pair.hpp>
1414

1515
#include <boost/hana/basic_tuple.hpp>
16+
#include <boost/hana/detail/as_container_element.hpp>
1617
#include <boost/hana/detail/intrinsics.hpp>
1718
#include <boost/hana/detail/operators/adl.hpp>
1819
#include <boost/hana/detail/operators/comparable.hpp>
@@ -120,8 +121,8 @@ namespace boost { namespace hana {
120121
struct make_impl<pair_tag> {
121122
template <typename F, typename S>
122123
static constexpr pair<
123-
typename std::decay<F>::type,
124-
typename std::decay<S>::type
124+
detail::as_container_element_t<F>,
125+
detail::as_container_element_t<S>
125126
> apply(F&& f, S&& s) {
126127
return {static_cast<F&&>(f), static_cast<S&&>(s)};
127128
}

test/pair/make.ref.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
@copyright Louis Dionne 2015
3+
Distributed under the Boost Software License, Version 1.0.
4+
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
5+
*/
6+
7+
#include <boost/hana/assert.hpp>
8+
#include <boost/hana/first.hpp>
9+
#include <boost/hana/pair.hpp>
10+
#include <boost/hana/second.hpp>
11+
12+
#include <functional>
13+
namespace hana = boost::hana;
14+
15+
16+
int main() {
17+
int i = 0;
18+
char c = 'x';
19+
hana::pair<int&, char const&> refs = hana::make_pair(std::ref(i), std::cref(c));
20+
21+
int& i_ref = hana::first(refs);
22+
BOOST_HANA_RUNTIME_CHECK(&i_ref == &i);
23+
24+
i_ref = 3;
25+
BOOST_HANA_RUNTIME_CHECK(i == 3);
26+
27+
char const& c_ref = hana::second(refs);
28+
BOOST_HANA_RUNTIME_CHECK(&c_ref == &c);
29+
BOOST_HANA_RUNTIME_CHECK(c == 'x');
30+
}

0 commit comments

Comments
 (0)