]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | |
2 | #ifdef _MSC_VER | |
3 | #pragma pack(2) | |
4 | #endif | |
5 | ||
6 | // (C) Copyright John Maddock 2000. | |
7 | // Use, modification and distribution are subject to the | |
8 | // Boost Software License, Version 1.0. (See accompanying file | |
9 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
10 | ||
7c673cae FG |
11 | #ifdef TEST_STD |
12 | # include <type_traits> | |
13 | #else | |
14 | # include <boost/type_traits/alignment_of.hpp> | |
15 | #endif | |
11fdf7f2 TL |
16 | #include "test.hpp" |
17 | #include "check_integral_constant.hpp" | |
7c673cae FG |
18 | |
19 | // | |
20 | // Need to defined some member functions for empty_UDT, | |
21 | // we don't want to put these in the test.hpp as that | |
22 | // causes overly-clever compilers to figure out that they can't throw | |
23 | // which in turn breaks other tests. | |
24 | // | |
25 | empty_UDT::empty_UDT(){} | |
26 | empty_UDT::~empty_UDT(){} | |
27 | empty_UDT::empty_UDT(const empty_UDT&){} | |
28 | empty_UDT& empty_UDT::operator=(const empty_UDT&){ return *this; } | |
29 | bool empty_UDT::operator==(const empty_UDT&)const{ return true; } | |
30 | ||
31 | ||
32 | // | |
33 | // VC++ emits an awful lot of warnings unless we define these: | |
34 | #ifdef BOOST_MSVC | |
35 | # pragma warning(disable:4244 4121) | |
36 | // | |
37 | // What follows here is the test case for issue 1946. | |
38 | // | |
39 | #include <boost/function.hpp> | |
40 | // This kind of packing is set within MSVC 9.0 headers. | |
41 | // E.g. std::ostream has it. | |
42 | #pragma pack(push,8) | |
43 | ||
44 | // The issue is gone if Root has no data members | |
45 | struct Root { int a; }; | |
46 | // The issue is gone if Root is inherited non-virtually | |
47 | struct A : virtual public Root {}; | |
48 | ||
49 | #pragma pack(pop) | |
50 | // | |
51 | // This class has 8-byte alignment but is 44 bytes in size, which means | |
52 | // that elements in an array of this type will not actually be 8 byte | |
53 | // aligned. This appears to be an MSVC bug, and throws off our | |
54 | // alignment calculations: causing us to report a non-sensical 12-byte | |
55 | // alignment for this type. This is fixed by using the native __alignof | |
56 | // operator. | |
57 | // | |
58 | class issue1946 : | |
59 | public A | |
60 | { | |
61 | public: | |
62 | // The issue is gone if the type is not a boost::function. The signature doesn't matter. | |
63 | typedef boost::function0< void > function_type; | |
64 | function_type m_function; | |
65 | }; | |
66 | ||
67 | #endif | |
68 | ||
69 | ||
70 | template <class T> | |
71 | struct align_calc | |
72 | { | |
73 | char padding; | |
74 | T instance; | |
75 | static std::ptrdiff_t get() | |
76 | { | |
77 | static align_calc<T> a; | |
78 | return reinterpret_cast<const char*>(&(a.instance)) - reinterpret_cast<const char*>(&(a.padding)); | |
79 | } | |
80 | }; | |
81 | ||
82 | #define ALIGNOF(x) align_calc< x>::get() | |
83 | ||
84 | TT_TEST_BEGIN(alignment_of) | |
85 | ||
86 | #ifndef TEST_STD | |
87 | // This test is not required to work for non-boost implementations: | |
88 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<void>::value, 0); | |
89 | #endif | |
90 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<char>::value, ALIGNOF(char)); | |
91 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<short>::value, ALIGNOF(short)); | |
92 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<int>::value, ALIGNOF(int)); | |
93 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<long>::value, ALIGNOF(long)); | |
94 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<float>::value, ALIGNOF(float)); | |
95 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<double>::value, ALIGNOF(double)); | |
96 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<long double>::value, ALIGNOF(long double)); | |
97 | #ifdef BOOST_HAS_LONG_LONG | |
98 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of< ::boost::long_long_type>::value, ALIGNOF(::boost::long_long_type)); | |
99 | #endif | |
100 | #ifdef BOOST_HAS_MS_INT64 | |
101 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<__int64>::value, ALIGNOF(__int64)); | |
102 | #endif | |
103 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<int[4]>::value, ALIGNOF(int[4])); | |
104 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<int(*)(int)>::value, ALIGNOF(int(*)(int))); | |
105 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<int*>::value, ALIGNOF(int*)); | |
106 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<VB>::value, ALIGNOF(VB)); | |
107 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<VD>::value, ALIGNOF(VD)); | |
108 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<enum_UDT>::value, ALIGNOF(enum_UDT)); | |
109 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<mf2>::value, ALIGNOF(mf2)); | |
110 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<POD_UDT>::value, ALIGNOF(POD_UDT)); | |
111 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<empty_UDT>::value, ALIGNOF(empty_UDT)); | |
112 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<union_UDT>::value, ALIGNOF(union_UDT)); | |
113 | ||
114 | #if defined(BOOST_MSVC) && (BOOST_MSVC >= 1400) | |
115 | BOOST_CHECK_INTEGRAL_CONSTANT(::tt::alignment_of<issue1946>::value, ALIGNOF(issue1946)); | |
116 | #endif | |
117 | ||
118 | TT_TEST_END | |
119 | ||
120 | ||
121 | ||
122 | ||
123 | ||
124 | ||
125 | ||
126 |