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 // class type_info 10 // 11 // bool operator==(const type_info& rhs) const noexcept; // constexpr since C++23 12 13 // UNSUPPORTED: no-rtti 14 15 // When we build for Windows on top of the VC runtime, `typeinfo::operator==` may not 16 // be `constexpr` (depending on the version of the VC runtime). So this test can fail. 17 // UNSUPPORTED: target={{.+}}-windows-msvc && !libcpp-no-vcruntime 18 19 #include <typeinfo> 20 #include <cassert> 21 22 #include "test_macros.h" 23 24 struct Base { funcBase25 virtual void func() {} 26 }; 27 struct Derived : Base { funcDerived28 virtual void func() {} 29 }; 30 test()31TEST_CONSTEXPR_CXX23 bool test() { 32 // Test when storing typeid() in a const ref 33 { 34 std::type_info const& t1 = typeid(int); 35 std::type_info const& t2 = typeid(long); 36 assert(t1 == t1); 37 assert(t2 == t2); 38 assert(t1 != t2); 39 } 40 41 // Test when using `typeid()` directly 42 { 43 struct Foo { }; 44 struct Bar { }; 45 assert(typeid(Foo) == typeid(Foo)); 46 assert(typeid(Foo) != typeid(Bar)); 47 } 48 49 // Test when using typeid(object) instead of typeid(type) 50 { 51 int x = 0, y = 0; 52 long z = 0; 53 assert(typeid(x) == typeid(y)); 54 assert(typeid(x) != typeid(z)); 55 } 56 57 // Check with derived/base types 58 { 59 Derived derived; 60 Base const& as_base = derived; 61 assert(typeid(as_base) == typeid(Derived)); 62 } 63 64 // Check noexcept-ness 65 { 66 std::type_info const& t1 = typeid(int); (void)t1; 67 std::type_info const& t2 = typeid(long); (void)t2; 68 ASSERT_NOEXCEPT(t1 == t2); 69 } 70 return true; 71 } 72 main(int,char **)73int main(int, char**) { 74 test(); 75 #if TEST_STD_VER >= 23 76 static_assert(test()); 77 #endif 78 return 0; 79 } 80