Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / functional / factory / test / factory_default_allocator.cpp
1 /*
2 Copyright 2019 Glen Joseph Fernandes
3 (glenjofe@gmail.com)
4
5 Distributed under the Boost Software License, Version 1.0.
6 (http://www.boost.org/LICENSE_1_0.txt)
7 */
8 #include <boost/functional/factory.hpp>
9 #include <boost/core/default_allocator.hpp>
10 #include <boost/core/lightweight_test.hpp>
11 #include <boost/smart_ptr/shared_ptr.hpp>
12 #include <boost/config.hpp>
13 #include <exception>
14
15 #if defined(BOOST_NO_EXCEPTIONS)
16 namespace boost {
17
18 BOOST_NORETURN void throw_exception(const std::exception&)
19 {
20     std::terminate();
21 }
22
23 }
24 #endif
25
26 class sum  {
27 public:
28     sum(int a, int b)
29         : value_(a + b) { }
30
31     int get() const {
32         return value_;
33     }
34
35 private:
36     int value_;
37 };
38
39 int main()
40 {
41     int a = 1;
42     int b = 2;
43     {
44         boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>,
45             boost::default_allocator<void>,
46             boost::factory_alloc_for_pointee_and_deleter>()(a, b));
47         BOOST_TEST(s->get() == 3);
48     }
49     {
50         boost::shared_ptr<sum> s(boost::factory<boost::shared_ptr<sum>,
51             boost::default_allocator<void>,
52             boost::factory_passes_alloc_to_smart_pointer>()(a, b));
53         BOOST_TEST(s->get() == 3);
54     }
55     return boost::report_errors();
56 }