]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/tutorial/integral-branching.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / hana / example / tutorial / integral-branching.cpp
1 // Copyright Louis Dionne 2013-2016
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)
4
5 #include <boost/hana.hpp>
6 #include <boost/hana/ext/std/integral_constant.hpp>
7
8 #include <memory>
9 #include <string>
10 #include <type_traits>
11 #include <utility>
12 namespace hana = boost::hana;
13
14
15
16 namespace ns1 {
17 //! [make_unique.if_]
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)...);
24 }
25 //! [make_unique.if_]
26 }
27
28
29 namespace ns2 {
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))...}); }
36 );
37 }
38 //! [make_unique.eval_if]
39 }
40
41 struct Student {
42 std::string name;
43 int age;
44 };
45
46 int main() {
47 {
48 std::unique_ptr<int> a = ns1::make_unique<int>(3);
49 std::unique_ptr<Student> b = ns1::make_unique<Student>("Bob", 25);
50 }
51 {
52 std::unique_ptr<int> a = ns2::make_unique<int>(3);
53 std::unique_ptr<Student> b = ns2::make_unique<Student>("Bob", 25);
54 }
55 }