openMSX
alignof.hh
Go to the documentation of this file.
1 #ifndef ALIGNOF_HH
2 #define ALIGNOF_HH
3 
4 // Portable alignof operator
5 //
6 // C++11 has a new alignof operator (much like the sizeof operator). Many
7 // compilers already offered a similar feature in non-c++11 mode as an
8 // extension (e.g. gcc has the __alignof__ operator). This file implements the
9 // same functionality for pre-c++11 compilers in a portable way. Once we switch
10 // to c++11 we can drop this header.
11 //
12 // Usage:
13 // c++11: alignof(SomeType)
14 // this: ALIGNOF(SomeType)
15 
16 template<typename T> struct AlignOf
17 {
18  struct S {
19  char c;
20  T t;
21  };
22  static const unsigned value = sizeof(S) - sizeof(T);
23 };
24 
25 #define ALIGNOF(T) AlignOf<T>::value
26 
27 
28 // c++11 offers std::max_align_t, a type whose alignment is at least as great
29 // as that of any standard type. The malloc() function guarantees to return
30 // memory with at least this alignment.
31 // gcc-4.7, gcc-4.8 : only offer ::max_align_t but not std::max_align_t
32 // gcc-4.9 : offers both ::max_align_t and std::max_align_t
33 // visual studio 2013: offers only std::max_align_t (as per the c++ standard)
35 {
36  long long ll;
37  long double ld;
38 };
39 
40 #endif
char c
Definition: alignof.hh:19
long double ld
Definition: alignof.hh:37
long long ll
Definition: alignof.hh:36
static const unsigned value
Definition: alignof.hh:22