Debian Codegraph @

gcc/testsuite/g++.dg/conversion/op8.C

raw
1 // PR c++/118282
2 // { dg-do "compile" }
3
4 #if __cplusplus >= 201103L
5 # include <cstdint> // Only available from c++11 onwards.
6 #endif
7
8 struct A {
9 explicit A (int);
10 operator void* () const;
11 };
12
13 struct B {
14 explicit B (int);
15 operator char* () const;
16 };
17
18 struct C {
19 explicit C (int);
20 operator int () const;
21 };
22
23 struct BothWays {
24 BothWays (int);
25 operator void*() const;
26 };
27
28 extern bool my_bool;
29
30 void foo (const A& a, const B& b, const C& c, const BothWays& d) {
31 void *res_a_1 = 0 ? 0 : a;
32 void *res_a_2 = 1 ? 0 : a;
33 void *res_a_3 = my_bool ? 0 : a;
34 void *res_a_4 = 0 ? a : 0;
35 void *res_a_5 = 1 ? a : 0;
36 void *res_a_6 = my_bool ? a : 0;
37
38 void *res_b_1 = 0 ? 0 : b;
39 void *res_b_2 = 1 ? 0 : b;
40 void *res_b_3 = my_bool ? 0 : b;
41 void *res_b_4 = 0 ? b : 0;
42 void *res_b_5 = 1 ? b : 0;
43 void *res_b_6 = my_bool ? b : 0;
44
45 //
46 // 0 valued constants that are NOT null pointer constants - this worked already.
47 //
48 char zero_char = 0;
49 void *res_ko1 = 0 ? zero_char : a; // { dg-error "different types" }
50
51 #if __cplusplus >= 201103L
52 // Those are only available starting with c++11.
53 int8_t zero_i8 = 0;
54 void *res_ko2 = 0 ? zero_i8 : a; // { dg-error "different types" "" { target c++11 } }
55 uintptr_t zerop = 0;
56 void *res_ko3 = 0 ? zerop : a; // { dg-error "different types" "" { target c++11 } }
57 #endif
58
59 // Conversion to integer - this worked already.
60 int res_int = 0 ? 0 : c;
61
62 // Case where one arm is of class type that can be constructed from an
63 // integer and the other arm is a null pointer constant (inspired by
64 // g++.dg/template/cond5.C).
65 0 ? d : 0;
66 0 ? 0 : d;
67 }
68
69 int main(){
70 A a (5);
71 B b (42);
72 C c (43);
73 BothWays d (1982);
74 foo (a, b, c, d);
75 }