3 // Copyright Neil Groves 2014. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see http://www.boost.org/libs/range/
10 #ifndef BOOST_RANGE_DETAIL_DEFAULT_CONSTRUCTIBLE_UNARY_FN_HPP_INCLUDED
11 #define BOOST_RANGE_DETAIL_DEFAULT_CONSTRUCTIBLE_UNARY_FN_HPP_INCLUDED
13 #include <boost/optional/optional.hpp>
14 #include <boost/mpl/if.hpp>
15 #include <boost/type_traits/has_trivial_constructor.hpp>
19 namespace range_detail
22 template<typename F, typename R>
23 class default_constructible_unary_fn_wrapper
26 typedef R result_type;
28 default_constructible_unary_fn_wrapper()
31 default_constructible_unary_fn_wrapper(const F& source)
35 default_constructible_unary_fn_wrapper(const default_constructible_unary_fn_wrapper& source)
36 : m_impl(source.m_impl)
39 default_constructible_unary_fn_wrapper& operator=(const default_constructible_unary_fn_wrapper& source)
43 // Lambda are not copy/move assignable.
44 m_impl.emplace(*source.m_impl);
52 template<typename Arg>
53 R operator()(const Arg& arg) const
56 return (*m_impl)(arg);
58 template<typename Arg>
59 R operator()(Arg& arg) const
62 return (*m_impl)(arg);
65 boost::optional<F> m_impl;
68 template<typename F, typename R>
69 struct default_constructible_unary_fn_gen
71 typedef typename boost::mpl::if_<
72 boost::has_trivial_default_constructor<F>,
74 default_constructible_unary_fn_wrapper<F,R>
78 } // namespace range_detail
81 #endif // include guard