alignment_of_test.cpp 4.2 KB

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