3 #ifndef __CDS_DETAILS_ALIGNED_TYPE_H
4 #define __CDS_DETAILS_ALIGNED_TYPE_H
6 #include <cds/details/defs.h>
8 namespace cds { namespace details {
12 This meta-algorithm solves compiler problem when you need to declare a type \p T with alignment
13 equal to another type alignment. For example, the following declaration produces an error in Microsoft Visual Studio 2008 compiler:
15 typedef double my_double;
16 typedef __declspec(align( __alignof(my_double) )) int aligned_int;
18 In MS VS, the __declspec(align(N)) construction requires that N must be a integer constant (1, 2, 4 and so on)
19 but not an integer constant expression.
21 The result of this meta-algo is a type \p aligned_type<T,Alignment>::type that is \p T aligned by \p Alignment.
22 For example, with \p aligned_type the prevoius example will not generate an error:
24 typedef double my_double;
25 typedef typename cds::details::aligned_type<int, __alignof(my_double)>::type aligned_int;
27 and result of this declaration is equivalent to
29 typedef __declspec(align(8)) int aligned_int;
32 The \p Alignment template parameter must be a constant expression and its result must be power of two.
33 The maximum of its value is 1024.
35 See also \ref align_as
37 template <typename T, size_t Alignment>
39 #ifdef CDS_DOXYGEN_INVOKED
45 # define CDS_ALIGNED_TYPE_impl(nAlign) template <typename T> struct aligned_type<T,nAlign> { typedef CDS_TYPE_ALIGNMENT(nAlign) T type; }
46 CDS_ALIGNED_TYPE_impl(1);
47 CDS_ALIGNED_TYPE_impl(2);
48 CDS_ALIGNED_TYPE_impl(4);
49 CDS_ALIGNED_TYPE_impl(8);
50 CDS_ALIGNED_TYPE_impl(16);
51 CDS_ALIGNED_TYPE_impl(32);
52 CDS_ALIGNED_TYPE_impl(64);
53 CDS_ALIGNED_TYPE_impl(128);
54 CDS_ALIGNED_TYPE_impl(256);
55 CDS_ALIGNED_TYPE_impl(512);
56 CDS_ALIGNED_TYPE_impl(1024);
57 # undef CDS_ALIGNED_TYPE_impl
60 /** Alignment by example
62 This meta-algo is similar to \ref aligned_type <T, alignof(AlignAs)>.
64 For example, the following code
66 typedef typename cds::details::align_as<int, double>::type aligned_int;
68 declares type \p aligned_int which is \p int aligned like \p double.
70 See also: \ref aligned_type
72 template <typename T, typename AlignAs>
74 /// Result of meta-algo: type \p T aligned like type \p AlignAs
75 typedef typename aligned_type<T, alignof(AlignAs)>::type type;
78 }} // namespace cds::details
80 #endif // #ifndef __CDS_DETAILS_ALIGNED_TYPE_H