]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
8 | [section:alignment_of alignment_of] | |
9 | template <class T> | |
10 | struct alignment_of : public __integral_constant<std::size_t, ALIGNOF(T)> {}; | |
11 | ||
12 | __inherit Class template `alignment_of` inherits from | |
13 | `__integral_constant<std::size_t, ALIGNOF(T)>`, where `ALIGNOF(T)` is the | |
14 | alignment of type T. | |
15 | ||
16 | ['Note: strictly speaking you should only rely on | |
17 | the value of `ALIGNOF(T)` being a multiple of the true alignment of T, although | |
18 | in practice it does compute the correct value in all the cases we know about.] | |
19 | ||
20 | __header ` #include <boost/type_traits/alignment_of.hpp>` or ` #include <boost/type_traits.hpp>` | |
21 | ||
22 | __examples | |
23 | ||
24 | [:`alignment_of<int>` inherits from `__integral_constant<std::size_t, ALIGNOF(int)>`.] | |
25 | ||
26 | [:`alignment_of<char>::type` is the type `__integral_constant<std::size_t, ALIGNOF(char)>`.] | |
27 | ||
28 | [:`alignment_of<double>::value` is an integral constant | |
29 | expression with value `ALIGNOF(double)`.] | |
30 | ||
31 | [:`alignment_of<T>::value_type` is the type `std::size_t`.] | |
32 | ||
33 | [important | |
34 | Visual C++ users should note that MSVC has varying definitions of "alignment". | |
35 | For example consider the following code: | |
36 | ||
37 | `` | |
38 | typedef long long align_t; | |
39 | assert(boost::alignment_of<align_t>::value % 8 == 0); | |
40 | align_t a; | |
41 | assert(((std::uintptr_t)&a % 8) == 0); | |
42 | char c = 0; | |
43 | align_t a1; | |
44 | assert(((std::uintptr_t)&a1 % 8) == 0); | |
45 | `` | |
46 | ||
47 | In this code, even though `boost::alignment_of<align_t>` reports that `align_t` has 8-byte | |
48 | alignment, the final assert will fail for a 32-bit build because `a1` is not aligned on an | |
49 | 8 byte boundary. Note that had we used the MSVC intrinsic `__alignof` in place of `boost::alignment_of` | |
50 | we would still get the same result. In fact for MSVC alignment requirements (and promises) only really | |
51 | apply to dynamic storage, and not the stack. | |
52 | ] | |
53 | ||
54 | [endsect] | |
55 |