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