]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/tutorial/integral-branching.cpp
1 // Copyright Louis Dionne 2013-2017
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
5 #include <boost/hana.hpp>
6 #include <boost/hana/ext/std/integral_constant.hpp>
10 #include <type_traits>
12 namespace hana
= boost::hana
;
18 template <typename T
, typename
...Args
>
19 std::unique_ptr
<T
> make_unique(Args
&&... args
) {
20 return hana::if_(std::is_constructible
<T
, Args
...>{},
21 [](auto&& ...x
) { return std::unique_ptr
<T
>(new T(std::forward
<Args
>(x
)...)); },
22 [](auto&& ...x
) { return std::unique_ptr
<T
>(new T
{std::forward
<Args
>(x
)...}); }
23 )(std::forward
<Args
>(args
)...);
30 //! [make_unique.eval_if]
31 template <typename T
, typename
...Args
>
32 std::unique_ptr
<T
> make_unique(Args
&&... args
) {
33 return hana::eval_if(std::is_constructible
<T
, Args
...>{},
34 [&](auto _
) { return std::unique_ptr
<T
>(new T(std::forward
<Args
>(_(args
))...)); },
35 [&](auto _
) { return std::unique_ptr
<T
>(new T
{std::forward
<Args
>(_(args
))...}); }
38 //! [make_unique.eval_if]
48 std::unique_ptr
<int> a
= ns1::make_unique
<int>(3);
49 std::unique_ptr
<Student
> b
= ns1::make_unique
<Student
>("Bob", 25);
52 std::unique_ptr
<int> a
= ns2::make_unique
<int>(3);
53 std::unique_ptr
<Student
> b
= ns2::make_unique
<Student
>("Bob", 25);