test_strong_typedef.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_strong_typedef.cpp
  3. // (C) Copyright 2016 Ashish Sadanandan
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // should pass compilation
  8. #include <stdlib.h> // EXIT_SUCCESS
  9. #include <boost/config.hpp>
  10. #include <boost/serialization/strong_typedef.hpp>
  11. #include <boost/static_assert.hpp>
  12. #include <boost/type_traits/has_nothrow_assign.hpp>
  13. #include <boost/type_traits/has_nothrow_constructor.hpp>
  14. #include <boost/type_traits/has_nothrow_copy.hpp>
  15. ///////////////////////////////////////////////////////////////////////
  16. // Define a strong typedef for int.
  17. // The new type should be nothrow constructible and assignable.
  18. BOOST_STRONG_TYPEDEF(int, strong_int)
  19. #ifndef BOOST_HAS_NOTHROW_CONSTRUCTOR
  20. BOOST_STATIC_ASSERT(boost::has_nothrow_default_constructor<strong_int>::value);
  21. BOOST_STATIC_ASSERT(boost::has_nothrow_copy_constructor<strong_int>::value);
  22. BOOST_STATIC_ASSERT(boost::has_nothrow_assign<strong_int>::value);
  23. #endif
  24. ///////////////////////////////////////////////////////////////////////
  25. // strong_int can now be placed in another type, which can also be
  26. // nothrow constructible and assignable.
  27. struct type1
  28. {
  29. long some_long;
  30. strong_int sint;
  31. };
  32. #ifndef BOOST_HAS_NOTHROW_CONSTRUCTOR
  33. BOOST_STATIC_ASSERT(boost::has_nothrow_default_constructor<type1>::value);
  34. BOOST_STATIC_ASSERT(boost::has_nothrow_copy_constructor<type1>::value);
  35. BOOST_STATIC_ASSERT(boost::has_nothrow_assign<type1>::value);
  36. #endif
  37. ///////////////////////////////////////////////////////////////////////
  38. // Now define a type that throws, and a strong_typedef for it
  39. // The strong_typedef should also not have nothrow construction/assign.
  40. struct not_noexcept
  41. {
  42. not_noexcept() {}
  43. not_noexcept(not_noexcept const&) {}
  44. not_noexcept& operator=(not_noexcept const&) {return *this;}
  45. bool operator==(not_noexcept const&) const {return false;}
  46. bool operator<(not_noexcept const&) const {return false;}
  47. };
  48. BOOST_STRONG_TYPEDEF(not_noexcept, strong_not_noexcept)
  49. #ifndef BOOST_HAS_NOTHROW_CONSTRUCTOR
  50. BOOST_STATIC_ASSERT(! boost::has_nothrow_default_constructor<strong_not_noexcept>::value);
  51. BOOST_STATIC_ASSERT(! boost::has_nothrow_copy_constructor<strong_not_noexcept>::value);
  52. BOOST_STATIC_ASSERT(! boost::has_nothrow_assign<strong_not_noexcept>::value);
  53. #endif
  54. int main()
  55. {
  56. return EXIT_SUCCESS;
  57. }