Skip to content

Commit 05ff129

Browse files
committed
[optional] Add support for reference_wrapper in just()
1 parent 14ee44d commit 05ff129

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

include/boost/hana/fwd/optional.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ namespace boost { namespace hana {
147147
//! `make<optional_tag>(x)` is equivalent to `just(x)`. This is provided
148148
//! for consistency with the other `make<...>` functions.
149149
//!
150+
//! @note
151+
//! `make<optional_tag>` supports reference wrappers. When a reference
152+
//! wrapper is passed to it, the resulting `hana::optional` will hold a
153+
//! reference to the object instead of the object itself.
154+
//!
150155
//!
151156
//! Example
152157
//! -------

include/boost/hana/optional.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Distributed under the Boost Software License, Version 1.0.
1414

1515
#include <boost/hana/bool.hpp>
1616
#include <boost/hana/core/tag_of.hpp>
17+
#include <boost/hana/detail/as_container_element.hpp>
1718
#include <boost/hana/detail/operators/adl.hpp>
1819
#include <boost/hana/detail/operators/comparable.hpp>
1920
#include <boost/hana/detail/operators/monad.hpp>
@@ -72,14 +73,14 @@ namespace boost { namespace hana {
7273
constexpr T const& operator*() const& { return this->val; }
7374
constexpr T operator*() && { return std::move(this->val); }
7475

75-
constexpr T* operator->() & { return &this->val; }
76-
constexpr T const* operator->() const& { return &this->val; }
76+
constexpr auto operator->() & { return &this->val; }
77+
constexpr auto operator->() const& { return &this->val; }
7778
};
7879

7980
//! @cond
8081
template <typename T>
8182
constexpr auto make_just_t::operator()(T&& t) const {
82-
return optional<typename std::decay<T>::type>(
83+
return optional<detail::as_container_element_t<T>>(
8384
static_cast<T&&>(t)
8485
);
8586
}

test/optional/make.ref.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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/optional.hpp>
9+
10+
#include <functional>
11+
namespace hana = boost::hana;
12+
13+
14+
int main() {
15+
{
16+
int i = 0;
17+
auto ref = hana::just(std::ref(i));
18+
19+
int& i_ref = *ref;
20+
BOOST_HANA_RUNTIME_CHECK(&i_ref == &i);
21+
22+
i_ref = 3;
23+
BOOST_HANA_RUNTIME_CHECK(i == 3);
24+
}
25+
26+
{
27+
int const i = 4;
28+
auto ref = hana::just(std::cref(i));
29+
30+
int const& i_ref = *ref;
31+
BOOST_HANA_RUNTIME_CHECK(&i_ref == &i);
32+
BOOST_HANA_RUNTIME_CHECK(i == 4);
33+
}
34+
}

0 commit comments

Comments
 (0)