alignment_of.pass.cpp (2028B)
1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // type_traits 11 12 // alignment_of 13 14 #include <type_traits> 15 #include <cstdint> 16 17 #include "test_macros.h" 18 19 template <class T, unsigned A> 20 void test_alignment_of() 21 { 22 const unsigned AlignofResult = TEST_ALIGNOF(T); 23 static_assert( AlignofResult == A, "Golden value does not match result of alignof keyword"); 24 static_assert( std::alignment_of<T>::value == AlignofResult, ""); 25 static_assert( std::alignment_of<T>::value == A, ""); 26 static_assert( std::alignment_of<const T>::value == A, ""); 27 static_assert( std::alignment_of<volatile T>::value == A, ""); 28 static_assert( std::alignment_of<const volatile T>::value == A, ""); 29 #if TEST_STD_VER > 14 30 static_assert( std::alignment_of_v<T> == A, ""); 31 static_assert( std::alignment_of_v<const T> == A, ""); 32 static_assert( std::alignment_of_v<volatile T> == A, ""); 33 static_assert( std::alignment_of_v<const volatile T> == A, ""); 34 #endif 35 } 36 37 class Class 38 { 39 public: 40 ~Class(); 41 }; 42 43 int main() 44 { 45 test_alignment_of<int&, 4>(); 46 test_alignment_of<Class, 1>(); 47 test_alignment_of<int*, sizeof(intptr_t)>(); 48 test_alignment_of<const int*, sizeof(intptr_t)>(); 49 test_alignment_of<char[3], 1>(); 50 test_alignment_of<int, 4>(); 51 // The test case below is a hack. It's hard to detect what golden value 52 // we should expect. In most cases it should be 8. But in i386 builds 53 // with Clang >= 8 or GCC >= 8 the value is '4'. 54 test_alignment_of<double, TEST_ALIGNOF(double)>(); 55 #if (defined(__ppc__) && !defined(__ppc64__)) 56 test_alignment_of<bool, 4>(); // 32-bit PPC has four byte bool 57 #else 58 test_alignment_of<bool, 1>(); 59 #endif 60 test_alignment_of<unsigned, 4>(); 61 }