typedef unsigned short ushort; enum E { E0, E1 }; enum class EC : int { EC0, EC1 }; void ArithmeticConversions() { char c = 0; unsigned char uc = 0; short s = 0; unsigned short us = 0; int i = 0; unsigned int ui = 0; long l = 0; unsigned long ul = 0; long long ll = 0; unsigned long long ull = 0; float f = 0; double d = 0; wchar_t wc = 0; E e{}; EC ec{}; c = uc; c = (char)uc; c = char(uc); c = static_cast(uc); i = s; i = (int)s; i = int(s); i = static_cast(s); us = i; us = (unsigned short)i; us = ushort(i); us = static_cast(i); i = d; i = (int)d; i = int(d); i = static_cast(d); f = c; f = (float)c; f = float(c); f = static_cast(c); f = d; f = (float)d; f = float(d); f = static_cast(d); d = f; d = (double)f; d = double(f); d = static_cast(f); i = E0; i = e; i = static_cast(EC::EC0); i = static_cast(ec); e = static_cast(i); ec = static_cast(i); } struct S { int x; double y; }; void ConversionsToBool() { bool b = 0; int i = 0; double d = 0; void* p = nullptr; int S::* pmd = nullptr; if (b) { } else if ((bool)b) { } else if (i) { } else if (d) { } else if (p) { } else if (pmd) { } } struct Base { int b1; void BaseMethod(); }; struct Middle : Base { int m1; void MiddleMethod(); }; struct Derived : Middle { int d1; void DerivedMethod(); }; void HierarchyCasts() { Base b; Middle m; Derived d; Base* pb = &b; Middle* pm = &m; Derived* pd = &d; b = m; b = (Base)m; b = static_cast(m); pb = pm; pb = (Base*)pm; pb = static_cast(pm); pb = reinterpret_cast(pm); m = (Middle&)b; m = static_cast(b); pm = (Middle*)pb; pm = static_cast(pb); pm = reinterpret_cast(pb); b = d; b = (Base)d; b = static_cast(d); pb = pd; pb = (Base*)pd; pb = static_cast(pd); pb = reinterpret_cast(pd); d = (Derived&)b; d = static_cast(b); pd = (Derived*)pb; pd = static_cast(pb); pd = reinterpret_cast(pb); } void PTMCasts() { int Base::* pb = &Base::b1; void (Base::* pmfb)() = &Base::BaseMethod; int Middle::* pm = &Middle::m1; void (Middle::* pmfm)() = &Middle::MiddleMethod; int Derived::* pd = &Derived::d1; void (Derived::* pmfd)() = &Derived::DerivedMethod; pb = (int Base::*)pm; pmfb = (void (Base::*)())pmfm; pb = static_cast(pm); pmfb = static_cast(pmfm); pm = pb; pmfm = pmfb; pm = (int Middle::*)pb; pmfm = (void (Middle::*)())pmfb; pm = static_cast(pb); pmfm = static_cast(pmfb); pb = (int Base::*)pd; pmfb = (void (Base::*)())pmfd; pb = static_cast(pd); pmfb = static_cast(pmfd); pd = pb; pmfd = pmfb; pd = (int Derived::*)pb; pmfd = (void (Derived::*)())pmfb; pd = static_cast(pb); pmfd = static_cast(pmfb); } struct String { String(); String(const String&); ~String(); }; void Adjust() { const String& s1 = String(); // prvalue adjustment Base b; Derived d; const Base& rb = true ? b : d; // glvalue adjustment const Base& r = (Base&)s1; } void QualificationConversions() { const int* pc = nullptr; const volatile int* pcv = nullptr; pcv = pc; pc = const_cast(pcv); } void PointerIntegralConversions() { void* p = nullptr; long n = (long)p; n = reinterpret_cast(p); p = (void*)n; p = reinterpret_cast(n); } struct PolymorphicBase { virtual ~PolymorphicBase(); }; struct PolymorphicDerived : PolymorphicBase { }; void DynamicCast() { PolymorphicBase b; PolymorphicDerived d; PolymorphicBase* pb = &b; PolymorphicDerived* pd = &d; // These two casts were previously represented as BaseClassCasts because they were resolved at compile time, but the front-end no longer performs this optimization. pb = dynamic_cast(pd); PolymorphicBase& rb = dynamic_cast(d); pd = dynamic_cast(pb); PolymorphicDerived& rd = dynamic_cast(b); } void FuncPtrConversions(int(*pfn)(int), void* p) { p = (void*)pfn; pfn = (int(*)(int))p; } int Func(); void ConversionsToVoid() { int x; (void)x; static_cast(x); (void)Func(); static_cast(Func()); (void)1; static_cast(1); }