Skip to content

Commit

Permalink
[optional] Add support for reference_wrapper in just()
Browse files Browse the repository at this point in the history
  • Loading branch information
ldionne committed Sep 13, 2015
1 parent 051d636 commit e63d814
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
7 changes: 4 additions & 3 deletions include/boost/hana/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Distributed under the Boost Software License, Version 1.0.

#include <boost/hana/bool.hpp>
#include <boost/hana/core/tag_of.hpp>
#include <boost/hana/detail/as_container_element.hpp>
#include <boost/hana/detail/operators/adl.hpp>
#include <boost/hana/detail/operators/comparable.hpp>
#include <boost/hana/detail/operators/monad.hpp>
Expand Down Expand Up @@ -72,14 +73,14 @@ namespace boost { namespace hana {
constexpr T const& operator*() const& { return this->val; }
constexpr T operator*() && { return std::move(this->val); }

constexpr T* operator->() & { return &this->val; }
constexpr T const* operator->() const& { return &this->val; }
constexpr auto operator->() & { return &this->val; }
constexpr auto operator->() const& { return &this->val; }
};

//! @cond
template <typename T>
constexpr auto make_just_t::operator()(T&& t) const {
return optional<typename std::decay<T>::type>(
return optional<detail::as_container_element_t<T>>(
static_cast<T&&>(t)
);
}
Expand Down
34 changes: 34 additions & 0 deletions test/optional/make.ref.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
@copyright Louis Dionne 2015
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/assert.hpp>
#include <boost/hana/optional.hpp>

#include <functional>
namespace hana = boost::hana;


int main() {
{
int i = 0;
auto ref = hana::just(std::ref(i));

int& i_ref = *ref;
BOOST_HANA_RUNTIME_CHECK(&i_ref == &i);

i_ref = 3;
BOOST_HANA_RUNTIME_CHECK(i == 3);
}

{
int const i = 4;
auto ref = hana::just(std::cref(i));

int const& i_ref = *ref;
BOOST_HANA_RUNTIME_CHECK(&i_ref == &i);
BOOST_HANA_RUNTIME_CHECK(i == 4);
}
}

0 comments on commit e63d814

Please sign in to comment.