// Copyright Louis Dionne 2013-2017 // 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 #include #include #include #include #include namespace hana = boost::hana; namespace ns1 { //! [make_unique.if_] template std::unique_ptr make_unique(Args&&... args) { return hana::if_(std::is_constructible{}, [](auto&& ...x) { return std::unique_ptr(new T(std::forward(x)...)); }, [](auto&& ...x) { return std::unique_ptr(new T{std::forward(x)...}); } )(std::forward(args)...); } //! [make_unique.if_] } namespace ns2 { //! [make_unique.eval_if] template std::unique_ptr make_unique(Args&&... args) { return hana::eval_if(std::is_constructible{}, [&](auto _) { return std::unique_ptr(new T(std::forward(_(args))...)); }, [&](auto _) { return std::unique_ptr(new T{std::forward(_(args))...}); } ); } //! [make_unique.eval_if] } struct Student { std::string name; int age; }; int main() { { std::unique_ptr a = ns1::make_unique(3); std::unique_ptr b = ns1::make_unique("Bob", 25); } { std::unique_ptr a = ns2::make_unique(3); std::unique_ptr b = ns2::make_unique("Bob", 25); } }