Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // type_traits |
| 11 | |
| 12 | // alignment_of |
| 13 | |
| 14 | #include <type_traits> |
Howard Hinnant | 1f96a4d | 2013-07-15 18:09:11 +0000 | [diff] [blame] | 15 | #include <cstdint> |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 16 | |
| 17 | template <class T, unsigned A> |
| 18 | void test_alignment_of() |
| 19 | { |
| 20 | static_assert( std::alignment_of<T>::value == A, ""); |
| 21 | static_assert( std::alignment_of<const T>::value == A, ""); |
| 22 | static_assert( std::alignment_of<volatile T>::value == A, ""); |
| 23 | static_assert( std::alignment_of<const volatile T>::value == A, ""); |
| 24 | } |
| 25 | |
| 26 | class Class |
| 27 | { |
| 28 | public: |
| 29 | ~Class(); |
| 30 | }; |
| 31 | |
| 32 | int main() |
| 33 | { |
Howard Hinnant | 5544f7e | 2013-04-22 19:37:49 +0000 | [diff] [blame] | 34 | test_alignment_of<int&, 4>(); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 35 | test_alignment_of<Class, 1>(); |
Howard Hinnant | 1f96a4d | 2013-07-15 18:09:11 +0000 | [diff] [blame] | 36 | test_alignment_of<int*, sizeof(intptr_t)>(); |
| 37 | test_alignment_of<const int*, sizeof(intptr_t)>(); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 38 | test_alignment_of<char[3], 1>(); |
| 39 | test_alignment_of<int, 4>(); |
Howard Hinnant | 5544f7e | 2013-04-22 19:37:49 +0000 | [diff] [blame] | 40 | test_alignment_of<double, 8>(); |
Marshall Clow | e3c9d52 | 2014-01-18 03:41:54 +0000 | [diff] [blame] | 41 | #if (defined(__ppc__) && !defined(__ppc64__)) |
Marshall Clow | 38d9005 | 2014-10-18 11:03:33 +0000 | [diff] [blame] | 42 | test_alignment_of<bool, 4>(); // 32-bit PPC has four byte bool |
Marshall Clow | e3c9d52 | 2014-01-18 03:41:54 +0000 | [diff] [blame] | 43 | #else |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 44 | test_alignment_of<bool, 1>(); |
Marshall Clow | e3c9d52 | 2014-01-18 03:41:54 +0000 | [diff] [blame] | 45 | #endif |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 46 | test_alignment_of<unsigned, 4>(); |
| 47 | } |