1 // Copyright 2015-2017 Hans Dembinski
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <boost/core/lightweight_test.hpp>
8 #include <boost/histogram/detail/make_default.hpp>
9 #include <new>
10 #include <vector>
11
12 using namespace boost::histogram::detail;
13
14 template <class T>
15 struct allocator_with_state {
16 using value_type = T;
17
allocator_with_stateallocator_with_state18 allocator_with_state(int s = 0) : state(s) {}
19
20 template <class U>
allocator_with_stateallocator_with_state21 allocator_with_state(const allocator_with_state<U>& o) : state(o.state) {}
22
allocateallocator_with_state23 value_type* allocate(std::size_t n) {
24 return static_cast<value_type*>(::operator new(n * sizeof(T)));
25 }
deallocateallocator_with_state26 void deallocate(value_type* ptr, std::size_t) {
27 ::operator delete(static_cast<void*>(ptr));
28 }
29
30 template <class U>
operator ==allocator_with_state31 bool operator==(const allocator_with_state<U>&) const {
32 return true;
33 }
34
35 template <class U>
operator !=allocator_with_state36 bool operator!=(const allocator_with_state<U>&) const {
37 return false;
38 }
39
40 int state;
41 };
42
main()43 int main() {
44
45 using V = std::vector<int, allocator_with_state<int>>;
46 V a(3, 0, allocator_with_state<int>{42});
47 V b = make_default(a);
48 V c;
49 BOOST_TEST_EQ(a.size(), 3);
50 BOOST_TEST_EQ(b.size(), 0);
51 BOOST_TEST_EQ(c.size(), 0);
52 BOOST_TEST_EQ(a.get_allocator().state, 42);
53 BOOST_TEST_EQ(b.get_allocator().state, 42);
54 BOOST_TEST_EQ(c.get_allocator().state, 0);
55
56 return boost::report_errors();
57 }
58