xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/cxx1y-contextual-conversion-tweaks.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -verify -fsyntax-only %s
2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++1y %s -verify -DCXX1Y
3*f4a2713aSLionel Sambuc 
4*f4a2713aSLionel Sambuc // Explicit member declarations behave as in C++11.
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc namespace n3323_example {
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc   template <class T> class zero_init {
9*f4a2713aSLionel Sambuc   public:
zero_init()10*f4a2713aSLionel Sambuc     zero_init() : val(static_cast<T>(0)) {}
zero_init(T val)11*f4a2713aSLionel Sambuc     zero_init(T val) : val(val) {}
12*f4a2713aSLionel Sambuc 
operator T&()13*f4a2713aSLionel Sambuc     operator T &() { return val; }     //@13
operator T() const14*f4a2713aSLionel Sambuc     operator T() const { return val; } //@14
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc   private:
17*f4a2713aSLionel Sambuc     T val;
18*f4a2713aSLionel Sambuc   };
19*f4a2713aSLionel Sambuc 
Delete()20*f4a2713aSLionel Sambuc   void Delete() {
21*f4a2713aSLionel Sambuc     zero_init<int *> p;
22*f4a2713aSLionel Sambuc     p = new int(7);
23*f4a2713aSLionel Sambuc     delete p; //@23
24*f4a2713aSLionel Sambuc     delete (p + 0);
25*f4a2713aSLionel Sambuc     delete + p;
26*f4a2713aSLionel Sambuc   }
27*f4a2713aSLionel Sambuc 
Switch()28*f4a2713aSLionel Sambuc   void Switch() {
29*f4a2713aSLionel Sambuc     zero_init<int> i;
30*f4a2713aSLionel Sambuc     i = 7;
31*f4a2713aSLionel Sambuc     switch (i) {} // @31
32*f4a2713aSLionel Sambuc     switch (i + 0) {}
33*f4a2713aSLionel Sambuc     switch (+i) {}
34*f4a2713aSLionel Sambuc   }
35*f4a2713aSLionel Sambuc }
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc #ifdef CXX1Y
38*f4a2713aSLionel Sambuc #else
39*f4a2713aSLionel Sambuc //expected-error@23 {{ambiguous conversion of delete expression of type 'zero_init<int *>' to a pointer}}
40*f4a2713aSLionel Sambuc //expected-note@13 {{conversion to pointer type 'int *'}}
41*f4a2713aSLionel Sambuc //expected-note@14 {{conversion to pointer type 'int *'}}
42*f4a2713aSLionel Sambuc //expected-error@31 {{multiple conversions from switch condition type 'zero_init<int>' to an integral or enumeration type}}
43*f4a2713aSLionel Sambuc //expected-note@13 {{conversion to integral type 'int'}}
44*f4a2713aSLionel Sambuc //expected-note@14 {{conversion to integral type 'int'}}
45*f4a2713aSLionel Sambuc #endif
46*f4a2713aSLionel Sambuc 
47*f4a2713aSLionel Sambuc namespace extended_examples {
48*f4a2713aSLionel Sambuc 
49*f4a2713aSLionel Sambuc   struct A0 {
50*f4a2713aSLionel Sambuc     operator int();      // matching and viable
51*f4a2713aSLionel Sambuc   };
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc   struct A1 {
54*f4a2713aSLionel Sambuc     operator int() &&;   // matching and not viable
55*f4a2713aSLionel Sambuc   };
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc   struct A2 {
58*f4a2713aSLionel Sambuc     operator float();    // not matching
59*f4a2713aSLionel Sambuc   };
60*f4a2713aSLionel Sambuc 
61*f4a2713aSLionel Sambuc   struct A3 {
62*f4a2713aSLionel Sambuc     template<typename T> operator T();  // not matching (ambiguous anyway)
63*f4a2713aSLionel Sambuc   };
64*f4a2713aSLionel Sambuc 
65*f4a2713aSLionel Sambuc   struct A4 {
66*f4a2713aSLionel Sambuc     template<typename T> operator int();  // not matching (ambiguous anyway)
67*f4a2713aSLionel Sambuc   };
68*f4a2713aSLionel Sambuc 
69*f4a2713aSLionel Sambuc   struct B1 {
70*f4a2713aSLionel Sambuc     operator int() &&;  // @70
71*f4a2713aSLionel Sambuc     operator int();     // @71  -- duplicate declaration with different qualifier is not allowed
72*f4a2713aSLionel Sambuc   };
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc   struct B2 {
75*f4a2713aSLionel Sambuc     operator int() &&;  // matching but not viable
76*f4a2713aSLionel Sambuc     operator float();   // not matching
77*f4a2713aSLionel Sambuc   };
78*f4a2713aSLionel Sambuc 
foo(A0 a0,A1 a1,A2 a2,A3 a3,A4 a4,B2 b2)79*f4a2713aSLionel Sambuc   void foo(A0 a0, A1 a1, A2 a2, A3 a3, A4 a4, B2 b2) {
80*f4a2713aSLionel Sambuc     switch (a0) {}
81*f4a2713aSLionel Sambuc     switch (a1) {}     // @81 -- fails for different reasons
82*f4a2713aSLionel Sambuc     switch (a2) {}     // @82
83*f4a2713aSLionel Sambuc     switch (a3) {}     // @83
84*f4a2713aSLionel Sambuc     switch (a4) {}     // @84
85*f4a2713aSLionel Sambuc     switch (b2) {}     // @85 -- fails for different reasons
86*f4a2713aSLionel Sambuc   }
87*f4a2713aSLionel Sambuc }
88*f4a2713aSLionel Sambuc 
89*f4a2713aSLionel Sambuc //expected-error@71 {{cannot overload a member function without a ref-qualifier with a member function with ref-qualifier '&&'}}
90*f4a2713aSLionel Sambuc //expected-note@70 {{previous declaration is here}}
91*f4a2713aSLionel Sambuc //expected-error@82 {{statement requires expression of integer type ('extended_examples::A2' invalid)}}
92*f4a2713aSLionel Sambuc //expected-error@83 {{statement requires expression of integer type ('extended_examples::A3' invalid)}}
93*f4a2713aSLionel Sambuc //expected-error@84 {{statement requires expression of integer type ('extended_examples::A4' invalid)}}
94*f4a2713aSLionel Sambuc 
95*f4a2713aSLionel Sambuc #ifdef CXX1Y
96*f4a2713aSLionel Sambuc //expected-error@81 {{statement requires expression of integer type ('extended_examples::A1' invalid)}}
97*f4a2713aSLionel Sambuc //expected-error@85 {{statement requires expression of integer type ('extended_examples::B2' invalid)}}
98*f4a2713aSLionel Sambuc #else
99*f4a2713aSLionel Sambuc //expected-error@81 {{cannot initialize object parameter of type 'extended_examples::A1' with an expression of type 'extended_examples::A1'}}
100*f4a2713aSLionel Sambuc //expected-error@85 {{cannot initialize object parameter of type 'extended_examples::B2' with an expression of type 'extended_examples::B2'}}
101*f4a2713aSLionel Sambuc #endif
102*f4a2713aSLionel Sambuc 
103*f4a2713aSLionel Sambuc namespace extended_examples_cxx1y {
104*f4a2713aSLionel Sambuc 
105*f4a2713aSLionel Sambuc   struct A1 {   // leads to viable match in C++1y, and no viable match in C++11
106*f4a2713aSLionel Sambuc     operator int() &&;                  // matching but not viable
107*f4a2713aSLionel Sambuc     template <typename T> operator T(); // In C++1y: matching and viable, since disambiguated by L.100
108*f4a2713aSLionel Sambuc   };
109*f4a2713aSLionel Sambuc 
110*f4a2713aSLionel Sambuc   struct A2 {   // leads to ambiguity in C++1y, and no viable match in C++11
111*f4a2713aSLionel Sambuc     operator int() &&;                    // matching but not viable
112*f4a2713aSLionel Sambuc     template <typename T> operator int(); // In C++1y: matching but ambiguous (disambiguated by L.105).
113*f4a2713aSLionel Sambuc   };
114*f4a2713aSLionel Sambuc 
115*f4a2713aSLionel Sambuc   struct B1 {    // leads to one viable match in both cases
116*f4a2713aSLionel Sambuc     operator int();                  // matching and viable
117*f4a2713aSLionel Sambuc     template <typename T> operator T(); // In C++1y: matching and viable, since disambiguated by L.110
118*f4a2713aSLionel Sambuc   };
119*f4a2713aSLionel Sambuc 
120*f4a2713aSLionel Sambuc   struct B2 {    // leads to one viable match in both cases
121*f4a2713aSLionel Sambuc     operator int();                  // matching and viable
122*f4a2713aSLionel Sambuc     template <typename T> operator int(); // In C++1y: matching but ambiguous, since disambiguated by L.115
123*f4a2713aSLionel Sambuc   };
124*f4a2713aSLionel Sambuc 
125*f4a2713aSLionel Sambuc   struct C {    // leads to no match in both cases
126*f4a2713aSLionel Sambuc     operator float();                  // not matching
127*f4a2713aSLionel Sambuc     template <typename T> operator T(); // In C++1y: not matching, nor viable.
128*f4a2713aSLionel Sambuc   };
129*f4a2713aSLionel Sambuc 
130*f4a2713aSLionel Sambuc   struct D {   // leads to viable match in C++1y, and no viable match in C++11
131*f4a2713aSLionel Sambuc     operator int() &&;                  // matching but not viable
132*f4a2713aSLionel Sambuc     operator float();                   // not matching
133*f4a2713aSLionel Sambuc     template <typename T> operator T(); // In C++1y: matching and viable, since disambiguated by L.125
134*f4a2713aSLionel Sambuc   };
135*f4a2713aSLionel Sambuc 
136*f4a2713aSLionel Sambuc 
foo(A1 a1,A2 a2,B1 b1,B2 b2,C c,D d)137*f4a2713aSLionel Sambuc   void foo(A1 a1, A2 a2, B1 b1, B2 b2, C c, D d) {
138*f4a2713aSLionel Sambuc     switch (a1) {} // @138 --  should presumably call templated conversion operator to convert to int.
139*f4a2713aSLionel Sambuc     switch (a2) {} // @139
140*f4a2713aSLionel Sambuc     switch (b1) {}
141*f4a2713aSLionel Sambuc     switch (b2) {}
142*f4a2713aSLionel Sambuc     switch (c) {}  // @142
143*f4a2713aSLionel Sambuc     switch (d) {}  // @143
144*f4a2713aSLionel Sambuc   }
145*f4a2713aSLionel Sambuc }
146*f4a2713aSLionel Sambuc 
147*f4a2713aSLionel Sambuc //expected-error@142 {{statement requires expression of integer type ('extended_examples_cxx1y::C' invalid)}}
148*f4a2713aSLionel Sambuc 
149*f4a2713aSLionel Sambuc #ifdef CXX1Y
150*f4a2713aSLionel Sambuc //expected-error@139 {{statement requires expression of integer type ('extended_examples_cxx1y::A2' invalid)}}
151*f4a2713aSLionel Sambuc #else
152*f4a2713aSLionel Sambuc //expected-error@138 {{cannot initialize object parameter of type 'extended_examples_cxx1y::A1' with an expression of type 'extended_examples_cxx1y::A1'}}
153*f4a2713aSLionel Sambuc //expected-error@139 {{cannot initialize object parameter of type 'extended_examples_cxx1y::A2' with an expression of type 'extended_examples_cxx1y::A2'}}
154*f4a2713aSLionel Sambuc //expected-error@143 {{cannot initialize object parameter of type 'extended_examples_cxx1y::D' with an expression of type 'extended_examples_cxx1y::D'}}
155*f4a2713aSLionel Sambuc #endif
156*f4a2713aSLionel Sambuc 
157*f4a2713aSLionel Sambuc namespace extended_examples_array_bounds {
158*f4a2713aSLionel Sambuc 
159*f4a2713aSLionel Sambuc   typedef decltype(sizeof(int)) size_t;
160*f4a2713aSLionel Sambuc 
161*f4a2713aSLionel Sambuc   struct Foo {
162*f4a2713aSLionel Sambuc     operator size_t();   // @162
163*f4a2713aSLionel Sambuc     operator unsigned short();  // @163
164*f4a2713aSLionel Sambuc   };
165*f4a2713aSLionel Sambuc 
bar()166*f4a2713aSLionel Sambuc   void bar() {
167*f4a2713aSLionel Sambuc     Foo x;
168*f4a2713aSLionel Sambuc     int *p = new int[x];        // @168
169*f4a2713aSLionel Sambuc   }
170*f4a2713aSLionel Sambuc }
171*f4a2713aSLionel Sambuc 
172*f4a2713aSLionel Sambuc #ifdef CXX1Y
173*f4a2713aSLionel Sambuc #else
174*f4a2713aSLionel Sambuc //expected-error@168 {{ambiguous conversion of array size expression of type 'extended_examples_array_bounds::Foo' to an integral or enumeration type}}
175*f4a2713aSLionel Sambuc //expected-note@162 {{conversion to integral type 'size_t'}}
176*f4a2713aSLionel Sambuc //expected-note@163 {{conversion to integral type 'unsigned short' declared here}}
177*f4a2713aSLionel Sambuc #endif
178