Debian Codegraph @

gcc/testsuite/g++.dg/ext/is_convertible2.C

raw
1 // PR c++/106784
2 // { dg-do compile { target c++20 } }
3 // { dg-skip-if "requires hosted libstdc++ for string" { ! hostedlib } }
4 // Adapted from <https://en.cppreference.com/w/cpp/types/is_convertible>.
5
6 #include <string>
7 #include <string_view>
8
9 #define SA(X) static_assert((X),#X)
10
11 class E { public: template<class T> E(T&&) { } };
12
13 int main()
14 {
15 class A {};
16 class B : public A {};
17 class C {};
18 class D { public: operator C() { return c; } C c; };
19
20 SA(__is_convertible(B*, A*));
21 SA(!__is_convertible(A*, B*));
22 SA(__is_convertible(D, C));
23 SA(!__is_convertible(B*, C*));
24 SA(__is_convertible(A, E));
25
26 using std::operator ""s, std::operator ""sv;
27
28 auto stringify = []<typename T>(T x) {
29 if constexpr (std::is_convertible_v<T, std::string> or
30 std::is_convertible_v<T, std::string_view>) {
31 return x;
32 } else {
33 return std::to_string(x);
34 }
35 };
36
37 const char* three = "three";
38
39 SA(!__is_convertible(std::string_view, std::string));
40 SA(__is_convertible(std::string, std::string_view));
41
42 auto s1 = stringify("one"s);
43 auto s2 = stringify("two"sv);
44 auto s3 = stringify(three);
45 auto s4 = stringify(42);
46 auto s5 = stringify(42.);
47 }