alignment_of.qbk 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. [/
  2. Copyright 2007 John Maddock.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:alignment_of alignment_of]
  8. template <class T>
  9. struct alignment_of : public __integral_constant<std::size_t, ALIGNOF(T)> {};
  10. __inherit Class template `alignment_of` inherits from
  11. `__integral_constant<std::size_t, ALIGNOF(T)>`, where `ALIGNOF(T)` is the
  12. alignment of type T.
  13. ['Note: strictly speaking you should only rely on
  14. the value of `ALIGNOF(T)` being a multiple of the true alignment of T, although
  15. in practice it does compute the correct value in all the cases we know about.]
  16. __header ` #include <boost/type_traits/alignment_of.hpp>` or ` #include <boost/type_traits.hpp>`
  17. __examples
  18. [:`alignment_of<int>` inherits from `__integral_constant<std::size_t, ALIGNOF(int)>`.]
  19. [:`alignment_of<char>::type` is the type `__integral_constant<std::size_t, ALIGNOF(char)>`.]
  20. [:`alignment_of<double>::value` is an integral constant
  21. expression with value `ALIGNOF(double)`.]
  22. [:`alignment_of<T>::value_type` is the type `std::size_t`.]
  23. [important
  24. Visual C++ users should note that MSVC has varying definitions of "alignment".
  25. For example consider the following code:
  26. ``
  27. typedef long long align_t;
  28. assert(boost::alignment_of<align_t>::value % 8 == 0);
  29. align_t a;
  30. assert(((std::uintptr_t)&a % 8) == 0);
  31. char c = 0;
  32. align_t a1;
  33. assert(((std::uintptr_t)&a1 % 8) == 0);
  34. ``
  35. In this code, even though `boost::alignment_of<align_t>` reports that `align_t` has 8-byte
  36. alignment, the final assert will fail for a 32-bit build because `a1` is not aligned on an
  37. 8 byte boundary. Note that had we used the MSVC intrinsic `__alignof` in place of `boost::alignment_of`
  38. we would still get the same result. In fact for MSVC alignment requirements (and promises) only really
  39. apply to dynamic storage, and not the stack.
  40. ]
  41. [endsect]