namespace Constructor {
struct A {
A(int);
};
struct B {
explicit B(int);
};
B::B(int) { }
struct C {
void f(const A&);
void f(const B&);
};
void f(C c) {
c.f(10);
}
}
namespace Conversion {
struct A {
operator int();
explicit operator bool();
};
A::operator bool() { return false; }
struct B {
void f(int);
void f(bool);
};
void f(A a, B b) {
b.f(a);
}
void testExplicit()
{
class Y { }; expected-note {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'Conversion::Z' to 'Conversion::Y &&' for 1st argument}} \
expected-note {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'Conversion::Z' to 'const Conversion::Y &' for 1st argument}} \
expected-note {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'Conversion::Z' to 'Conversion::Y &&' for 1st argument}}
struct Z {
explicit operator Y() const;
explicit operator int() const;
};
Z z;
Y y2 = z; Y y3 = (Y)z;
Y y4 = Y(z);
Y y5 = static_cast<Y>(z);
int i1 = (int)z;
int i2 = int(z);
int i3 = static_cast<int>(z);
int i4(z);
const Y& y6 = z; const int& y7(z);
}
void testBool() {
struct Bool {
operator bool();
};
struct NotBool {
explicit operator bool(); };
Bool b;
NotBool n;
(void) (1 + b);
(void) (1 + n);
(void) (!b);
(void) (!n);
(void) (b && true);
(void) (n && true);
(void) (b || true);
(void) (n || true);
(void) (b ? 0 : 1);
(void) (n ? 0: 1);
if (b) {}
if (n) {}
switch (b) {} switch (n) {} expected-warning {{switch condition has boolean value}}
while (b) {}
while (n) {}
do {} while (b);
do {} while (n);
for (;b;) {}
for (;n;) {}
}
void testNew()
{
struct Int {
operator int();
};
struct NotInt {
explicit operator int(); };
Int i;
NotInt ni;
new int[i];
new int[ni]; }
void testDelete()
{
struct Ptr {
operator int*();
};
struct NotPtr {
explicit operator int*();
};
Ptr p;
NotPtr np;
delete p;
delete np; }
void testFunctionPointer()
{
using Func = void(*)(int);
struct FP {
operator Func();
};
struct NotFP {
explicit operator Func();
};
FP fp;
NotFP nfp;
fp(1);
nfp(1); }
}