xref: /llvm-project/clang/test/SemaCXX/conditional-expr.cpp (revision 1a99f441e64c853f5f7a029c4e159db8209aa685)
1 // RUN: clang-cc -fsyntax-only -verify -std=c++0x %s
2 
3 // C++ rules for ?: are a lot stricter than C rules, and have to take into
4 // account more conversion options.
5 // This test runs in C++0x mode for the contextual conversion of the condition.
6 
7 struct ToBool { explicit operator bool(); };
8 
9 struct B;
10 struct A { A(); A(const B&); };
11 struct B { operator A() const; };
12 struct I { operator int(); };
13 struct J { operator I(); };
14 struct K { operator double(); };
15 typedef void (*vfn)();
16 struct F { operator vfn(); };
17 struct G { operator vfn(); };
18 
19 struct Base {
20   int trick();
21   A trick() const;
22 };
23 struct Derived : Base {};
24 struct Convertible { operator Base&(); };
25 struct Priv : private Base {};
26 struct Mid : Base {};
27 struct Fin : Mid, Derived {};
28 
29 struct BadDerived;
30 struct BadBase { operator BadDerived&(); };
31 struct BadDerived : BadBase {};
32 
33 struct Fields {
34   int i1, i2, b1 : 3, b2 : 3;
35 };
36 
37 enum Enum { EVal };
38 
39 struct Ambig {
40   operator short();
41   operator signed char();
42 };
43 
44 void test()
45 {
46   // This function tests C++0x 5.16
47 
48   // p1 (contextually convert to bool)
49   int i1 = ToBool() ? 0 : 1;
50 
51   // p2 (one or both void, and throwing)
52   i1 ? throw 0 : throw 1;
53   i1 ? test() : throw 1;
54   i1 ? throw 0 : test();
55   i1 ? test() : test();
56   i1 = i1 ? throw 0 : 0;
57   i1 = i1 ? 0 : throw 0;
58   i1 ? 0 : test(); // expected-error {{right operand to ? is void, but left operand is of type 'int'}}
59   i1 ? test() : 0; // expected-error {{left operand to ? is void, but right operand is of type 'int'}}
60   (i1 ? throw 0 : i1) = 0; // expected-error {{expression is not assignable}}
61   (i1 ? i1 : throw 0) = 0; // expected-error {{expression is not assignable}}
62 
63   // p3 (one or both class type, convert to each other)
64   // b1 (lvalues)
65   Base base;
66   Derived derived;
67   Convertible conv;
68   // FIXME: lvalueness
69   /*Base &bar1 =*/(void)( i1 ? base : derived);
70   /*Base &bar2 =*/(void)( i1 ? derived : base);
71   /*Base &bar3 =*/(void)( i1 ? base : conv);
72   /*Base &bar4 =*/(void)( i1 ? conv : base);
73   // these are ambiguous
74   BadBase bb;
75   BadDerived bd;
76   (void)(i1 ? bb : bd); // expected-error {{conditional expression is ambiguous; 'struct BadBase' can be converted to 'struct BadDerived' and vice versa}}
77   (void)(i1 ? bd : bb); // expected-error {{conditional expression is ambiguous}}
78   // curiously enough (and a defect?), these are not
79   // for rvalues, hierarchy takes precedence over other conversions
80   (void)(i1 ? BadBase() : BadDerived());
81   (void)(i1 ? BadDerived() : BadBase());
82 
83   // b2.1 (hierarchy stuff)
84   const Base constret();
85   const Derived constder();
86   // should use const overload
87   A a1((i1 ? constret() : Base()).trick());
88   A a2((i1 ? Base() : constret()).trick());
89   A a3((i1 ? constret() : Derived()).trick());
90   A a4((i1 ? Derived() : constret()).trick());
91   // should use non-const overload
92   i1 = (i1 ? Base() : Base()).trick();
93   i1 = (i1 ? Base() : Base()).trick();
94   i1 = (i1 ? Base() : Derived()).trick();
95   i1 = (i1 ? Derived() : Base()).trick();
96   // should fail: const lost
97   (void)(i1 ? Base() : constder()); // expected-error {{incompatible operand types ('struct Base' and 'struct Derived const')}}
98   (void)(i1 ? constder() : Base()); // expected-error {{incompatible operand types ('struct Derived const' and 'struct Base')}}
99   // should fail: private or ambiguous base
100   (void)(i1 ? Base() : Priv()); // xpected-error private base
101   (void)(i1 ? Priv() : Base()); // xpected-error private base
102   (void)(i1 ? Base() : Fin()); // xpected-error ambiguous base
103   (void)(i1 ? Fin() : Base()); // xpected-error ambiguous base
104 
105   // b2.2 (non-hierarchy)
106   i1 = i1 ? I() : i1;
107   i1 = i1 ? i1 : I();
108   I i2(i1 ? I() : J());
109   I i3(i1 ? J() : I());
110   // "the type [it] woud have if E2 were converted to an rvalue"
111   vfn pfn = i1 ? F() : test;
112   pfn = i1 ? test : F();
113   // these are ambiguous - better messages would be nice
114   (void)(i1 ? A() : B()); // expected-error {{incompatible operand types}}
115   (void)(i1 ? B() : A()); // expected-error {{incompatible operand types}}
116   (void)(i1 ? 1 : Ambig()); // expected-error {{incompatible operand types}}
117   (void)(i1 ? Ambig() : 1); // expected-error {{incompatible operand types}}
118 
119   // p4 (lvalue, same type)
120   //Fields flds;
121   int &ir1 = i1;
122   //int &ir1 = i1 ? flds.i1 : flds.i2;
123   //(i1 ? flds.b1 : flds.i2) = 0;
124   //(i1 ? flds.i1 : flds.b2) = 0;
125   //(i1 ? flds.b1 : flds.b2) = 0;
126 
127   // p5 (conversion to built-in types)
128   // GCC 4.3 fails these
129   double d1 = i1 ? I() : K();
130   pfn = i1 ? F() : G();
131 
132   // p6 (final conversions)
133   i1 = i1 ? i1 : ir1;
134   int *pi1 = i1 ? &i1 : 0;
135   pi1 = i1 ? 0 : &i1;
136   i1 = i1 ? i1 : EVal;
137   i1 = i1 ? EVal : i1;
138   d1 = i1 ? 'c' : 4.0;
139   d1 = i1 ? 4.0 : 'c';
140 
141   // Note the thing that this does not test: since DR446, various situations
142   // *must* create a separate temporary copy of class objects. This can only
143   // be properly tested at runtime, though.
144 }
145