alignment_of_a2_test.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifdef _MSC_VER
  2. #pragma pack(2)
  3. #endif
  4. // (C) Copyright John Maddock 2000.
  5. // Use, modification and distribution are subject to the
  6. // Boost Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifdef TEST_STD
  9. # include <type_traits>
  10. #else
  11. # include <boost/type_traits/alignment_of.hpp>
  12. #endif
  13. #include "test.hpp"
  14. #include "check_integral_constant.hpp"
  15. //
  16. // Need to defined some member functions for empty_UDT,
  17. // we don't want to put these in the test.hpp as that
  18. // causes overly-clever compilers to figure out that they can't throw
  19. // which in turn breaks other tests.
  20. //
  21. empty_UDT::empty_UDT(){}
  22. empty_UDT::~empty_UDT(){}
  23. empty_UDT::empty_UDT(const empty_UDT&){}
  24. empty_UDT& empty_UDT::operator=(const empty_UDT&){ return *this; }
  25. bool empty_UDT::operator==(const empty_UDT&)const{ return true; }
  26. //
  27. // VC++ emits an awful lot of warnings unless we define these:
  28. #ifdef BOOST_MSVC
  29. # pragma warning(disable:4244 4121)
  30. //
  31. // What follows here is the test case for issue 1946.
  32. //
  33. #include <boost/function.hpp>
  34. // This kind of packing is set within MSVC 9.0 headers.
  35. // E.g. std::ostream has it.
  36. #pragma pack(push,8)
  37. // The issue is gone if Root has no data members
  38. struct Root { int a; };
  39. // The issue is gone if Root is inherited non-virtually
  40. struct A : virtual public Root {};
  41. #pragma pack(pop)
  42. //
  43. // This class has 8-byte alignment but is 44 bytes in size, which means
  44. // that elements in an array of this type will not actually be 8 byte
  45. // aligned. This appears to be an MSVC bug, and throws off our
  46. // alignment calculations: causing us to report a non-sensical 12-byte
  47. // alignment for this type. This is fixed by using the native __alignof
  48. // operator.
  49. //
  50. class issue1946 :
  51. public A
  52. {
  53. public:
  54. // The issue is gone if the type is not a boost::function. The signature doesn't matter.
  55. typedef boost::function0< void > function_type;
  56. function_type m_function;
  57. };
  58. #endif
  59. template <class T>
  60. struct align_calc
  61. {
  62. char padding;
  63. T instance;
  64. static std::ptrdiff_t get()
  65. {
  66. static align_calc<T> a;
  67. return reinterpret_cast<const char*>(&(a.instance)) - reinterpret_cast<const char*>(&(a.padding));
  68. }
  69. };
  70. #define ALIGNOF(x) align_calc< x>::get()
  71. TT_TEST_BEGIN(alignment_of)
  72. #ifndef TEST_STD
  73. // This test is not required to work for non-boost implementations:
  74. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<void>::value, 0);
  75. #endif
  76. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<char>::value, ALIGNOF(char));
  77. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<short>::value, ALIGNOF(short));
  78. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<int>::value, ALIGNOF(int));
  79. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<long>::value, ALIGNOF(long));
  80. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<float>::value, ALIGNOF(float));
  81. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<double>::value, ALIGNOF(double));
  82. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<long double>::value, ALIGNOF(long double));
  83. #ifdef BOOST_HAS_LONG_LONG
  84. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of< ::boost::long_long_type>::value, ALIGNOF(::boost::long_long_type));
  85. #endif
  86. #ifdef BOOST_HAS_MS_INT64
  87. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<__int64>::value, ALIGNOF(__int64));
  88. #endif
  89. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<int[4]>::value, ALIGNOF(int[4]));
  90. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<int(*)(int)>::value, ALIGNOF(int(*)(int)));
  91. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<int*>::value, ALIGNOF(int*));
  92. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<VB>::value, ALIGNOF(VB));
  93. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<VD>::value, ALIGNOF(VD));
  94. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<enum_UDT>::value, ALIGNOF(enum_UDT));
  95. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<mf2>::value, ALIGNOF(mf2));
  96. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<POD_UDT>::value, ALIGNOF(POD_UDT));
  97. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<empty_UDT>::value, ALIGNOF(empty_UDT));
  98. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<union_UDT>::value, ALIGNOF(union_UDT));
  99. #if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400)
  100. BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<issue1946>::value, ALIGNOF(issue1946));
  101. #endif
  102. TT_TEST_END