allocate_unique_aggregate_test.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright 2019 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #include <boost/config.hpp>
  8. #if (!defined(BOOST_LIBSTDCXX_VERSION) || \
  9. BOOST_LIBSTDCXX_VERSION >= 46000) && \
  10. !defined(BOOST_NO_CXX11_SMART_PTR) && \
  11. !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  12. #include <boost/smart_ptr/allocate_unique.hpp>
  13. #include <boost/core/lightweight_test.hpp>
  14. template<class T = void>
  15. struct creator {
  16. typedef T value_type;
  17. typedef T* pointer;
  18. template<class U>
  19. struct rebind {
  20. typedef creator<U> other;
  21. };
  22. creator() { }
  23. template<class U>
  24. creator(const creator<U>&) { }
  25. T* allocate(std::size_t size) {
  26. return static_cast<T*>(::operator new(sizeof(T) * size));
  27. }
  28. void deallocate(T* ptr, std::size_t) {
  29. ::operator delete(ptr);
  30. }
  31. };
  32. template<class T, class U>
  33. inline bool
  34. operator==(const creator<T>&, const creator<U>&)
  35. {
  36. return true;
  37. }
  38. template<class T, class U>
  39. inline bool
  40. operator!=(const creator<T>&, const creator<U>&)
  41. {
  42. return false;
  43. }
  44. struct type {
  45. int x;
  46. int y;
  47. };
  48. int main()
  49. {
  50. {
  51. std::unique_ptr<type,
  52. boost::alloc_deleter<type, creator<type> > > result =
  53. boost::allocate_unique<type>(creator<type>(), { 1, 2 });
  54. BOOST_TEST(result.get() != 0);
  55. BOOST_TEST(result->x == 1);
  56. BOOST_TEST(result->y == 2);
  57. }
  58. {
  59. std::unique_ptr<const type,
  60. boost::alloc_deleter<const type, creator<> > > result =
  61. boost::allocate_unique<const type>(creator<>(), { 1, 2 });
  62. BOOST_TEST(result.get() != 0);
  63. BOOST_TEST(result->x == 1);
  64. BOOST_TEST(result->y == 2);
  65. }
  66. return boost::report_errors();
  67. }
  68. #else
  69. int main()
  70. {
  71. return 0;
  72. }
  73. #endif