1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
// These compilers don't support __builtin_is_virtual_base_of yet.
// UNSUPPORTED: clang-18, clang-19, gcc-14, apple-clang-16, apple-clang-17
// <type_traits>
// std::is_virtual_base_of
#include <type_traits>
#include <cassert>
template <bool expected, class Base, class Derived>
void test() {
// Test the type of the variables
{
static_assert(std::is_same_v<bool const, decltype(std::is_virtual_base_of<Base, Derived>::value)>);
static_assert(std::is_same_v<bool const, decltype(std::is_virtual_base_of_v<Base, Derived>)>);
}
// Test their value
{
static_assert(std::is_virtual_base_of<Base, Derived>::value == expected);
static_assert(std::is_virtual_base_of<const Base, Derived>::value == expected);
static_assert(std::is_virtual_base_of<Base, const Derived>::value == expected);
static_assert(std::is_virtual_base_of<const Base, const Derived>::value == expected);
static_assert(std::is_virtual_base_of_v<Base, Derived> == expected);
static_assert(std::is_virtual_base_of_v<const Base, Derived> == expected);
static_assert(std::is_virtual_base_of_v<Base, const Derived> == expected);
static_assert(std::is_virtual_base_of_v<const Base, const Derived> == expected);
}
// Check the relationship with is_base_of. If it's not a base of, it can't be a virtual base of.
{ static_assert(!std::is_base_of_v<Base, Derived> ? !std::is_virtual_base_of_v<Base, Derived> : true); }
// Make sure they can be referenced at runtime
{
bool const& a = std::is_virtual_base_of<Base, Derived>::value;
bool const& b = std::is_virtual_base_of_v<Base, Derived>;
assert(a == expected);
assert(b == expected);
}
}
struct Incomplete;
struct Unrelated {};
union IncompleteUnion;
union Union {
int i;
float f;
};
class Base {};
class Derived : Base {};
class Derived2 : Base {};
class Derived2a : Derived {};
class Derived2b : Derived {};
class Derived3Virtual : virtual Derived2a, virtual Derived2b {};
struct DerivedTransitiveViaNonVirtual : Derived3Virtual {};
struct DerivedTransitiveViaVirtual : virtual Derived3Virtual {};
template <typename T>
struct CrazyDerived : T {};
template <typename T>
struct CrazyDerivedVirtual : virtual T {};
struct DerivedPrivate : private virtual Base {};
struct DerivedProtected : protected virtual Base {};
struct DerivedPrivatePrivate : private DerivedPrivate {};
struct DerivedPrivateProtected : private DerivedProtected {};
struct DerivedProtectedPrivate : protected DerivedProtected {};
struct DerivedProtectedProtected : protected DerivedProtected {};
struct DerivedTransitivePrivate : private Derived, private Derived2 {};
int main(int, char**) {
// Test with non-virtual inheritance
{
test<false, Base, Base>();
test<false, Base, Derived>();
test<false, Base, Derived2>();
test<false, Derived, DerivedTransitivePrivate>();
test<false, Derived, Base>();
test<false, Incomplete, Derived>();
// Derived must be a complete type if Base and Derived are non-union class types
// test<false, Base, Incomplete>();
}
// Test with virtual inheritance
{
test<false, Base, Derived3Virtual>();
test<false, Derived, Derived3Virtual>();
test<true, Derived2b, Derived3Virtual>();
test<true, Derived2a, Derived3Virtual>();
test<true, Base, DerivedPrivate>();
test<true, Base, DerivedProtected>();
test<true, Base, DerivedPrivatePrivate>();
test<true, Base, DerivedPrivateProtected>();
test<true, Base, DerivedProtectedPrivate>();
test<true, Base, DerivedProtectedProtected>();
test<true, Derived2a, DerivedTransitiveViaNonVirtual>();
test<true, Derived2b, DerivedTransitiveViaNonVirtual>();
test<true, Derived2a, DerivedTransitiveViaVirtual>();
test<true, Derived2b, DerivedTransitiveViaVirtual>();
test<false, Base, CrazyDerived<Base>>();
test<false, CrazyDerived<Base>, Base>();
test<true, Base, CrazyDerivedVirtual<Base>>();
test<false, CrazyDerivedVirtual<Base>, Base>();
}
// Test unrelated types
{
test<false, Base&, Derived&>();
test<false, Base[3], Derived[3]>();
test<false, Unrelated, Derived>();
test<false, Base, Unrelated>();
test<false, Base, void>();
test<false, void, Derived>();
}
// Test scalar types
{
test<false, int, Base>();
test<false, int, Derived>();
test<false, int, Incomplete>();
test<false, int, int>();
test<false, Base, int>();
test<false, Derived, int>();
test<false, Incomplete, int>();
test<false, int[], int[]>();
test<false, long, int>();
test<false, int, long>();
}
// Test unions
{
test<false, Union, Union>();
test<false, IncompleteUnion, IncompleteUnion>();
test<false, Union, IncompleteUnion>();
test<false, IncompleteUnion, Union>();
test<false, Incomplete, IncompleteUnion>();
test<false, IncompleteUnion, Incomplete>();
test<false, Unrelated, IncompleteUnion>();
test<false, IncompleteUnion, Unrelated>();
test<false, int, IncompleteUnion>();
test<false, IncompleteUnion, int>();
test<false, Unrelated, Union>();
test<false, Union, Unrelated>();
test<false, int, Unrelated>();
test<false, Union, int>();
}
return 0;
}
|