aligned_storage.qbk 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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:aligned_storage aligned_storage]
  8. template <std::size_t Size, std::size_t Align>
  9. struct aligned_storage
  10. {
  11. typedef __below type;
  12. };
  13. __type a built-in or POD type with size `Size` and an alignment
  14. that is a multiple of `Align`.
  15. __header ` #include <boost/type_traits/aligned_storage.hpp>` or ` #include <boost/type_traits.hpp>`
  16. On the GCC and Visual C++ compilers (or compilers that are compatible with them), we support
  17. requests for types with alignments greater than any built in type (up to 128-bit alignment).
  18. Visual C++ users should note that such "extended" types can not be passed down the stack as
  19. by-value function arguments.
  20. [important
  21. Visual C++ users should be aware that MSVC has an elastic definition of alignment, for
  22. example consider the following code:
  23. ``
  24. typedef boost::aligned_storage<8,8>::type align_t;
  25. assert(boost::alignment_of<align_t>::value % 8 == 0);
  26. align_t a;
  27. assert(((std::uintptr_t)&a % 8) == 0);
  28. char c = 0;
  29. align_t a1;
  30. assert(((std::uintptr_t)&a1 % 8) == 0);
  31. ``
  32. In this code the final assert will fail for a 32-bit build because variable `a1` is not
  33. aligned on an 8-byte boundary. Had we used the MSVC intrinsic `__alignof` in
  34. place of `alignment_of` or `std::aligned_storage` in place of `boost::aligned_storage`
  35. the result would have been no different. In MSVC alignment requirements/promises only
  36. really apply to variables on the heap, not on the stack.
  37. Further, although MSVC has a mechanism for generating new types with arbitrary alignment
  38. requirements, such types cannot be passed as function arguments on the program stack.
  39. Therefore had `boost::aligned_storage<8,8>::type` been a type declared with
  40. `__declspec(align(8))` we would break a great deal of existing code that relies on
  41. being able to pass such types through the program stack.
  42. ]
  43. [endsect]