size_and_alignment.pass.cpp (1671B)
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 // <array> 11 12 // template <class T, size_t N > 13 // struct array 14 15 // Test the size and alignment matches that of an array of a given type. 16 17 #include <array> 18 #include <iterator> 19 #include <type_traits> 20 #include <cstddef> 21 22 #include "test_macros.h" 23 24 25 template <class T, size_t Size> 26 struct MyArray { 27 T elems[Size]; 28 }; 29 30 template <class T, size_t Size> 31 void test() { 32 typedef T CArrayT[Size == 0 ? 1 : Size]; 33 typedef std::array<T, Size> ArrayT; 34 typedef MyArray<T, Size == 0 ? 1 : Size> MyArrayT; 35 static_assert(sizeof(ArrayT) == sizeof(CArrayT), ""); 36 static_assert(sizeof(ArrayT) == sizeof(MyArrayT), ""); 37 static_assert(TEST_ALIGNOF(ArrayT) == TEST_ALIGNOF(MyArrayT), ""); 38 #if defined(_LIBCPP_VERSION) 39 ArrayT a; 40 ((void)a); 41 static_assert(sizeof(ArrayT) == sizeof(a.__elems_), ""); 42 static_assert(TEST_ALIGNOF(ArrayT) == __alignof__(a.__elems_), ""); 43 #endif 44 } 45 46 template <class T> 47 void test_type() { 48 test<T, 1>(); 49 test<T, 42>(); 50 test<T, 0>(); 51 } 52 53 struct TEST_ALIGNAS(TEST_ALIGNOF(std::max_align_t) * 2) TestType1 { 54 55 }; 56 57 struct TEST_ALIGNAS(TEST_ALIGNOF(std::max_align_t) * 2) TestType2 { 58 char data[1000]; 59 }; 60 61 int main() { 62 test_type<char>(); 63 test_type<int>(); 64 test_type<double>(); 65 test_type<long double>(); 66 test_type<std::max_align_t>(); 67 test_type<TestType1>(); 68 test_type<TestType2>(); 69 }