alignment_of.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // (C) Copyright John Maddock 2000.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt).
  5. //
  6. // See http://www.boost.org/libs/type_traits for most recent version including documentation.
  7. #ifndef BOOST_TT_ALIGNMENT_OF_HPP_INCLUDED
  8. #define BOOST_TT_ALIGNMENT_OF_HPP_INCLUDED
  9. #include <boost/config.hpp>
  10. #include <cstddef>
  11. #include <boost/type_traits/intrinsics.hpp>
  12. #include <boost/type_traits/integral_constant.hpp>
  13. #ifdef BOOST_MSVC
  14. # pragma warning(push)
  15. # pragma warning(disable: 4121 4512) // alignment is sensitive to packing
  16. #endif
  17. #if defined(__BORLANDC__) && (__BORLANDC__ < 0x600)
  18. #pragma option push -Vx- -Ve-
  19. #endif
  20. namespace boost {
  21. template <typename T> struct alignment_of;
  22. // get the alignment of some arbitrary type:
  23. namespace detail {
  24. #ifdef BOOST_MSVC
  25. #pragma warning(push)
  26. #pragma warning(disable:4324) // structure was padded due to __declspec(align())
  27. #endif
  28. template <typename T>
  29. struct alignment_of_hack
  30. {
  31. char c;
  32. T t;
  33. alignment_of_hack();
  34. };
  35. #ifdef BOOST_MSVC
  36. #pragma warning(pop)
  37. #endif
  38. template <unsigned A, unsigned S>
  39. struct alignment_logic
  40. {
  41. BOOST_STATIC_CONSTANT(std::size_t, value = A < S ? A : S);
  42. };
  43. template< typename T >
  44. struct alignment_of_impl
  45. {
  46. #if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400)
  47. //
  48. // With MSVC both the native __alignof operator
  49. // and our own logic gets things wrong from time to time :-(
  50. // Using a combination of the two seems to make the most of a bad job:
  51. //
  52. BOOST_STATIC_CONSTANT(std::size_t, value =
  53. (::boost::detail::alignment_logic<
  54. sizeof(::boost::detail::alignment_of_hack<T>) - sizeof(T),
  55. __alignof(T)
  56. >::value));
  57. #elif !defined(BOOST_ALIGNMENT_OF)
  58. BOOST_STATIC_CONSTANT(std::size_t, value =
  59. (::boost::detail::alignment_logic<
  60. sizeof(::boost::detail::alignment_of_hack<T>) - sizeof(T),
  61. sizeof(T)
  62. >::value));
  63. #else
  64. //
  65. // We put this here, rather than in the definition of
  66. // alignment_of below, because MSVC's __alignof doesn't
  67. // always work in that context for some unexplained reason.
  68. // (See type_with_alignment tests for test cases).
  69. //
  70. BOOST_STATIC_CONSTANT(std::size_t, value = BOOST_ALIGNMENT_OF(T));
  71. #endif
  72. };
  73. } // namespace detail
  74. template <class T> struct alignment_of : public integral_constant<std::size_t, ::boost::detail::alignment_of_impl<T>::value>{};
  75. // references have to be treated specially, assume
  76. // that a reference is just a special pointer:
  77. template <typename T> struct alignment_of<T&> : public alignment_of<T*>{};
  78. #ifdef __BORLANDC__
  79. // long double gives an incorrect value of 10 (!)
  80. // unless we do this...
  81. struct long_double_wrapper{ long double ld; };
  82. template<> struct alignment_of<long double> : public alignment_of<long_double_wrapper>{};
  83. #endif
  84. // void has to be treated specially:
  85. template<> struct alignment_of<void> : integral_constant<std::size_t, 0>{};
  86. #ifndef BOOST_NO_CV_VOID_SPECIALIZATIONS
  87. template<> struct alignment_of<void const> : integral_constant<std::size_t, 0>{};
  88. template<> struct alignment_of<void const volatile> : integral_constant<std::size_t, 0>{};
  89. template<> struct alignment_of<void volatile> : integral_constant<std::size_t, 0>{};
  90. #endif
  91. } // namespace boost
  92. #if defined(__BORLANDC__) && (__BORLANDC__ < 0x600)
  93. #pragma option pop
  94. #endif
  95. #ifdef BOOST_MSVC
  96. # pragma warning(pop)
  97. #endif
  98. #endif // BOOST_TT_ALIGNMENT_OF_HPP_INCLUDED