Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/explicit5.C

raw
1 // P0892R2
2 // { dg-do compile }
3 // Explicit { dg-require-effective-target exceptions_enabled } to avoid verify compiler messages FAILs for '-fno-exceptions'.
4 // { dg-options "-std=c++2a" }
5
6 constexpr int fn0 () { return 0; }
7 constexpr int fn1 () { return 1; }
8
9 struct S0 {
10 explicit(false) operator int();
11 explicit(1 == 0) operator double();
12 explicit(fn0()) operator char();
13 };
14
15 struct S1 {
16 explicit(true) operator int();
17 explicit(1 == 1) operator double();
18 explicit(fn1()) operator char();
19 };
20
21 struct X {
22 static const bool value = true;
23 static constexpr bool foo () { return 1; }
24 };
25
26 struct T {
27 explicit(true ? 1 : throw 1) operator int();
28 explicit(true || true ? 1 : throw 1) operator double();
29 explicit(X::value) operator char();
30 explicit(X::foo ()) operator long();
31 };
32
33 struct W {
34 constexpr operator bool() { return true; };
35 };
36
37 struct W2 {
38 constexpr operator bool() { return false; };
39 };
40
41 struct U1 {
42 explicit(W()) operator int();
43 };
44
45 struct U2 {
46 explicit(W2()) operator int();
47 };
48
49 int
50 main ()
51 {
52 S0 s0;
53 S1 s1;
54 int i0 = s0;
55 int i1 = s1; // { dg-error "cannot convert" }
56 double d0 = s0;
57 double d1 = s1; // { dg-error "cannot convert" }
58 char c0 = s0;
59 char c1 = s1; // { dg-error "cannot convert" }
60
61 T t;
62 int i2 = t; // { dg-error "cannot convert" }
63 double d2 = t; // { dg-error "cannot convert" }
64 char c2 = t; // { dg-error "cannot convert" }
65 long l1 = t; // { dg-error "cannot convert" }
66
67 U1 u1;
68 int i3 = u1; // { dg-error "cannot convert" }
69
70 U2 u2;
71 int i4 = u2;
72 }