1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // type_traits 10 11 // is_base_of 12 13 #include <type_traits> 14 15 #include "test_macros.h" 16 17 // Clang before v9 and apple-clang up to and including v11 do not 18 // report that unions are never base classes - nor can they have bases. 19 // See https://reviews.llvm.org/D61858 20 // XFAIL: apple-clang-9.0, apple-clang-9.1, apple-clang-10.0, apple-clang-11.0.0 21 // XFAIL: clang-4.0, clang-5.0, clang-6.0, clang-7.0, clang-8.0 22 23 24 template <class T, class U> 25 void test_is_base_of() 26 { 27 static_assert((std::is_base_of<T, U>::value), ""); 28 static_assert((std::is_base_of<const T, U>::value), ""); 29 static_assert((std::is_base_of<T, const U>::value), ""); 30 static_assert((std::is_base_of<const T, const U>::value), ""); 31 #if TEST_STD_VER > 14 32 static_assert((std::is_base_of_v<T, U>), ""); 33 static_assert((std::is_base_of_v<const T, U>), ""); 34 static_assert((std::is_base_of_v<T, const U>), ""); 35 static_assert((std::is_base_of_v<const T, const U>), ""); 36 #endif 37 } 38 39 template <class T, class U> 40 void test_is_not_base_of() 41 { 42 static_assert((!std::is_base_of<T, U>::value), ""); 43 } 44 45 struct B {}; 46 struct B1 : B {}; 47 struct B2 : B {}; 48 struct D : private B1, private B2 {}; 49 union U0; 50 union U1 {}; 51 struct I0; 52 struct I1 {}; 53 54 int main(int, char**) 55 { 56 // A union is never the base class of anything (including incomplete types) 57 test_is_not_base_of<U0, B>(); 58 test_is_not_base_of<U0, B1>(); 59 test_is_not_base_of<U0, B2>(); 60 test_is_not_base_of<U0, D>(); 61 test_is_not_base_of<U1, B>(); 62 test_is_not_base_of<U1, B1>(); 63 test_is_not_base_of<U1, B2>(); 64 test_is_not_base_of<U1, D>(); 65 test_is_not_base_of<U0, I0>(); 66 test_is_not_base_of<U1, I1>(); 67 test_is_not_base_of<U0, U1>(); 68 test_is_not_base_of<U0, int>(); 69 test_is_not_base_of<U1, int>(); 70 test_is_not_base_of<I0, int>(); 71 test_is_not_base_of<I1, int>(); 72 73 // A union never has base classes (including incomplete types) 74 test_is_not_base_of<B, U0>(); 75 test_is_not_base_of<B1, U0>(); 76 test_is_not_base_of<B2, U0>(); 77 test_is_not_base_of<D, U0>(); 78 test_is_not_base_of<B, U1>(); 79 test_is_not_base_of<B1, U1>(); 80 test_is_not_base_of<B2, U1>(); 81 test_is_not_base_of<D, U1>(); 82 test_is_not_base_of<I0, U0>(); 83 test_is_not_base_of<I1, U1>(); 84 test_is_not_base_of<U1, U0>(); 85 test_is_not_base_of<int, U0>(); 86 test_is_not_base_of<int, U1>(); 87 test_is_not_base_of<int, I0>(); 88 test_is_not_base_of<int, I1>(); 89 90 return 0; 91 } 92