detail_make_default_test.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #include <boost/core/lightweight_test.hpp>
  7. #include <boost/histogram/detail/make_default.hpp>
  8. #include <new>
  9. #include <vector>
  10. using namespace boost::histogram::detail;
  11. template <class T>
  12. struct allocator_with_state {
  13. using value_type = T;
  14. allocator_with_state(int s = 0) : state(s) {}
  15. template <class U>
  16. allocator_with_state(const allocator_with_state<U>& o) : state(o.state) {}
  17. value_type* allocate(std::size_t n) {
  18. return static_cast<value_type*>(::operator new(n * sizeof(T)));
  19. }
  20. void deallocate(value_type* ptr, std::size_t) {
  21. ::operator delete(static_cast<void*>(ptr));
  22. }
  23. template <class U>
  24. bool operator==(const allocator_with_state<U>&) const {
  25. return true;
  26. }
  27. template <class U>
  28. bool operator!=(const allocator_with_state<U>&) const {
  29. return false;
  30. }
  31. int state;
  32. };
  33. int main() {
  34. using V = std::vector<int, allocator_with_state<int>>;
  35. V a(3, 0, allocator_with_state<int>{42});
  36. V b = make_default(a);
  37. V c;
  38. BOOST_TEST_EQ(a.size(), 3);
  39. BOOST_TEST_EQ(b.size(), 0);
  40. BOOST_TEST_EQ(c.size(), 0);
  41. BOOST_TEST_EQ(a.get_allocator().state, 42);
  42. BOOST_TEST_EQ(b.get_allocator().state, 42);
  43. BOOST_TEST_EQ(c.get_allocator().state, 0);
  44. return boost::report_errors();
  45. }