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
17template <class T, class U>
18void test_is_base_of()
19{
20 static_assert((std::is_base_of<T, U>::value), "");
21 static_assert((std::is_base_of<const T, U>::value), "");
22 static_assert((std::is_base_of<T, const U>::value), "");
23 static_assert((std::is_base_of<const T, const U>::value), "");
24#if TEST_STD_VER > 14
25 static_assert((std::is_base_of_v<T, U>), "");
26 static_assert((std::is_base_of_v<const T, U>), "");
27 static_assert((std::is_base_of_v<T, const U>), "");
28 static_assert((std::is_base_of_v<const T, const U>), "");
29#endif
30}
31
32template <class T, class U>
33void test_is_not_base_of()
34{
35 static_assert((!std::is_base_of<T, U>::value), "");
36}
37
38struct B {};
39struct B1 : B {};
40struct B2 : B {};
41struct D : private B1, private B2 {};
42struct I0; // incomplete
43
44int main(int, char**)
45{
46 test_is_base_of<B, D>();
47 test_is_base_of<B1, D>();
48 test_is_base_of<B2, D>();
49 test_is_base_of<B, B1>();
50 test_is_base_of<B, B2>();
51 test_is_base_of<B, B>();
52
53 test_is_not_base_of<D, B>();
54 test_is_not_base_of<B&, D&>();
55 test_is_not_base_of<B[3], D[3]>();
56 test_is_not_base_of<int, int>();
57
58// A scalar is never the base class of anything (including incomplete types)
59 test_is_not_base_of<int, B>();
60 test_is_not_base_of<int, B1>();
61 test_is_not_base_of<int, B2>();
62 test_is_not_base_of<int, D>();
63 test_is_not_base_of<int, I0>();
64
65// A scalar never has base classes (including incomplete types)
66 test_is_not_base_of<B, int>();
67 test_is_not_base_of<B1, int>();
68 test_is_not_base_of<B2, int>();
69 test_is_not_base_of<D, int>();
70 test_is_not_base_of<I0, int>();
71
72 return 0;
73}
74

source code of libcxx/test/std/utilities/meta/meta.rel/is_base_of.pass.cpp