| 1 | // Copyright 2023 Peter Dimov |
| 2 | // Distributed under the Boost Software License, Version 1.0. |
| 3 | // https://www.boost.org/LICENSE_1_0.txt |
| 4 | |
| 5 | #include <boost/core/alignof.hpp> |
| 6 | #include <boost/core/lightweight_test.hpp> |
| 7 | #include <boost/type_traits.hpp> |
| 8 | #include <boost/config.hpp> |
| 9 | #include <cstddef> |
| 10 | |
| 11 | template<class T> struct struct_of |
| 12 | { |
| 13 | T t; |
| 14 | }; |
| 15 | |
| 16 | template<class T> union union_of |
| 17 | { |
| 18 | T t; |
| 19 | }; |
| 20 | |
| 21 | template<class T> void test2() |
| 22 | { |
| 23 | BOOST_TEST_EQ( BOOST_CORE_ALIGNOF(T), boost::alignment_of<T>::value ); |
| 24 | |
| 25 | #if !defined(BOOST_NO_CXX11_ALIGNOF) |
| 26 | |
| 27 | BOOST_TEST_EQ( BOOST_CORE_ALIGNOF(T), alignof(T) ); |
| 28 | |
| 29 | #endif |
| 30 | } |
| 31 | |
| 32 | template<class T> void test() |
| 33 | { |
| 34 | test2<T>(); |
| 35 | test2<T[2]>(); |
| 36 | test2< struct_of<T> >(); |
| 37 | test2< union_of<T> >(); |
| 38 | } |
| 39 | |
| 40 | struct X |
| 41 | { |
| 42 | }; |
| 43 | |
| 44 | int main() |
| 45 | { |
| 46 | test<char>(); |
| 47 | test<short>(); |
| 48 | test<int>(); |
| 49 | test<long>(); |
| 50 | |
| 51 | #if !defined(BOOST_NO_LONG_LONG) |
| 52 | # if !( defined(__GNUC__) && defined(__i386__) ) |
| 53 | |
| 54 | // g++ -m32 has alignof(long long) = 8, but boost::alignment_of<long long>::value = 4 |
| 55 | test<boost::long_long_type>(); |
| 56 | |
| 57 | # endif |
| 58 | #endif |
| 59 | |
| 60 | #if defined(BOOST_HAS_INT128) |
| 61 | |
| 62 | test<boost::int128_type>(); |
| 63 | |
| 64 | #endif |
| 65 | |
| 66 | test<float>(); |
| 67 | |
| 68 | #if !( defined(__GNUC__) && defined(__i386__) ) |
| 69 | |
| 70 | // g++ -m32 has alignof(double) = 8, but boost::alignment_of<double>::value = 4 |
| 71 | test<double>(); |
| 72 | |
| 73 | #endif |
| 74 | |
| 75 | test<long double>(); |
| 76 | |
| 77 | #if defined(BOOST_HAS_FLOAT128) |
| 78 | |
| 79 | test<__float128>(); |
| 80 | |
| 81 | #endif |
| 82 | |
| 83 | test<void*>(); |
| 84 | test<void(*)()>(); |
| 85 | |
| 86 | #if !defined(_MSC_VER) |
| 87 | |
| 88 | // under MSVC, alignof is 8, boost::alignment_of is 4 |
| 89 | // under clang-cl, alignof is 4, boost::alignment_of is 8 (!) |
| 90 | |
| 91 | test<int X::*>(); |
| 92 | |
| 93 | #endif |
| 94 | |
| 95 | test<void (X::*)()>(); |
| 96 | |
| 97 | return boost::report_errors(); |
| 98 | } |
| 99 | |