1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++2a %s
4 namespace Constructor
{
9 struct B
{ // expected-note+ {{candidate}}
10 explicit B(int); // expected-note {{not a candidate}}
13 B::B(int) { } // expected-note+ {{here}}
32 B b0
= 0; // expected-error {{no viable conversion}}
34 B
&&b2
= 0; // expected-error {{could not bind}}
35 B
&&b3(0); // expected-error {{could not bind}}
37 B
&&b5
= {0}; // expected-error {{chosen constructor is explicit}}
41 template <bool b
= true>
56 namespace Conversion
{
59 explicit operator bool();
62 A::operator bool() { return false; }
75 // Taken from 12.3.2p2
77 class Y
{ }; // expected-note+ {{candidate constructor (the implicit}}
80 explicit operator X() const;
81 explicit operator Y() const; // expected-note 2{{not a candidate}}
82 explicit operator int() const; // expected-note {{not a candidate}}
86 // 13.3.1.4p1 & 8.5p16:
87 Y y2
= z
; // expected-error {{no viable conversion from 'Z' to 'Y'}}
91 Y y5
= static_cast<Y
>(z
);
92 // 13.3.1.5p1 & 8.5p16:
95 int i3
= static_cast<int>(z
);
97 // 13.3.1.6p1 & 8.5.3p5:
98 const Y
& y6
= z
; // expected-error {{no viable conversion from 'Z' to 'const Y'}}
99 const int& y7
= z
; // expected-error {{no viable conversion from 'Z' to 'const int'}}
103 // Y is an aggregate, so aggregate-initialization is performed and the
104 // conversion function is not considered.
105 const Y y10
{z
}; // expected-error {{excess elements}}
106 const Y
& y11
{z
}; // expected-error {{excess elements}} expected-note {{in initialization of temporary of type 'const Y'}}
109 // X is not an aggregate, so constructors are considered,
110 // per 13.3.3.1/4 & DR1467.
121 explicit operator bool(); // expected-note {{conversion to integral type 'bool'}} expected-note 4{{explicit conversion function is not a candidate}}
127 (void) (1 + n
); // expected-error {{invalid operands to binary expression ('int' and 'NotBool')}}
146 // TODO: After constexpr has been implemented
153 switch (b
) {} // expected-warning {{switch condition has boolean value}}
154 switch (n
) {} // expected-error {{switch condition type 'NotBool' requires explicit conversion to 'bool'}} \
155 expected
-warning
{{switch condition has boolean value
}}
173 int direct4(n
); // expected-error {{no viable conversion}}
174 const bool &direct5(b
);
175 const bool &direct6(n
);
176 const int &direct7(b
);
177 const int &direct8(n
); // expected-error {{no viable conversion}}
181 int directList4
{n
}; // expected-error {{no viable conversion}}
182 const bool &directList5
{b
};
183 const bool &directList6
{n
};
184 const int &directList7
{b
};
185 const int &directList8
{n
}; // expected-error {{no viable conversion}}
187 bool copy2
= n
; // expected-error {{no viable conversion}}
189 int copy4
= n
; // expected-error {{no viable conversion}}
190 const bool ©5
= b
;
191 const bool ©6
= n
; // expected-error {{no viable conversion}}
192 const int ©7
= b
;
193 const int ©8
= n
; // expected-error {{no viable conversion}}
194 bool copyList1
= {b
};
195 bool copyList2
= {n
}; // expected-error {{no viable conversion}}
197 int copyList4
= {n
}; // expected-error {{no viable conversion}}
198 const bool ©List5
= {b
};
199 const bool ©List6
= {n
}; // expected-error {{no viable conversion}}
200 const int ©List7
= {b
};
201 const int ©List8
= {n
}; // expected-error {{no viable conversion}}
204 #if __cplusplus < 201707L
212 explicit operator int(); // expected-note {{conversion to integral type 'int' declared here}}
219 new int[ni
]; // expected-error {{array size expression of type 'NotInt' requires explicit conversion to type 'int'}}
230 explicit operator int*(); // expected-note {{conversion}}
237 delete np
; // expected-error {{converting delete expression from type 'NotPtr' to type 'int *' invokes an explicit conversion function}}
240 void testFunctionPointer()
243 using Func
= void(*)(int);
249 explicit operator Func();
255 nfp(1); // expected-error {{type 'NotFP' does not provide a call operator}}
261 explicit explicit Test(int x
); // expected-warning{{duplicate 'explicit' declaration specifier}}
266 struct S
{ explicit operator bool() const; } s
;
267 int *p
= new int(s
); // expected-error {{no viable conversion}}
270 namespace DoubleDiags
{
271 struct ExplicitConvert
{
272 explicit operator int();//#DOUBLE_DIAG_OP_INT
276 // expected-error@+2{{switch condition type 'struct ExplicitConvert' requires explicit conversion to 'int'}}
277 // expected-note@#DOUBLE_DIAG_OP_INT{{conversion to integral type 'int'}}