// RUN: clang -fsyntax-only -verify %s struct A {}; struct B : public A {}; // Single public base. struct C1 : public virtual B {}; // Single virtual base. struct C2 : public virtual B {}; struct D : public C1, public C2 {}; // Diamond struct E : private A {}; // Single private base. struct F : public C1 {}; // Single path to B with virtual. struct G1 : public B {}; struct G2 : public B {}; struct H : public G1, public G2 {}; // Ambiguous path to B. enum Enum { En1, En2 }; enum Onom { On1, On2 }; // Explicit implicits void t_529_2() { int i = 1; (void)static_cast(i); double d = 1.0; (void)static_cast(d); (void)static_cast(d); (void)static_cast(i); (void)static_cast(i); (void)static_cast(En1); (void)static_cast(En1); (void)static_cast(i); (void)static_cast(i); int ar[1]; (void)static_cast(ar); (void)static_cast(t_529_2); (void)static_cast(0); (void)static_cast((int*)0); (void)static_cast((const int*)0); (void)static_cast((B*)0); (void)static_cast(*((B*)0)); (void)static_cast((C1*)0); (void)static_cast(*((C1*)0)); (void)static_cast((D*)0); (void)static_cast(*((D*)0)); // TODO: User-defined conversions // Bad code below (void)static_cast((const int*)0); // expected-error {{static_cast from 'int const *' to 'void *' is not allowed}} //(void)static_cast((E*)0); // {{static_cast from 'struct E *' to 'struct A *' is not allowed}} //(void)static_cast((H*)0); // {{static_cast from 'struct H *' to 'struct A *' is not allowed}} (void)static_cast((int*)0); // expected-error {{static_cast from 'int *' to 'int' is not allowed}} (void)static_cast((B**)0); // expected-error {{static_cast from 'struct B **' to 'struct A **' is not allowed}} (void)static_cast(i); // expected-error {{non-const reference to type 'char' cannot be initialized with a value of type 'int'}} } // Anything to void void t_529_4() { static_cast(1); static_cast(t_529_4); } // Static downcasts void t_529_5_8() { (void)static_cast((A*)0); (void)static_cast(*((A*)0)); (void)static_cast((A*)0); (void)static_cast(*((A*)0)); // Bad code below (void)static_cast((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct C1 *' via virtual base 'struct B'}} (void)static_cast(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct C1 &' via virtual base 'struct B'}} (void)static_cast((A*)0); // expected-error {{cannot cast 'struct A *' to 'struct D *' via virtual base 'struct B'}} (void)static_cast(*((A*)0)); // expected-error {{cannot cast 'struct A' to 'struct D &' via virtual base 'struct B'}} (void)static_cast((const A*)0); // expected-error {{static_cast from 'struct A const *' to 'struct B *' casts away constness}} (void)static_cast(*((const A*)0)); // expected-error {{static_cast from 'struct A const' to 'struct B &' casts away constness}} // Accessibility is not yet tested //(void)static_cast((A*)0); // {{static_cast from 'struct A *' to 'struct E *' is not allowed}} //(void)static_cast(*((A*)0)); // {{static_cast from 'struct A' to 'struct E &' is not allowed}} (void)static_cast((A*)0); // expected-error {{ambiguous static_cast from base 'struct A' to derived 'struct H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}} (void)static_cast(*((A*)0)); // expected-error {{ambiguous static_cast from base 'struct A' to derived 'struct H':\n struct A -> struct B -> struct G1 -> struct H\n struct A -> struct B -> struct G2 -> struct H}} (void)static_cast((B*)0); // expected-error {{static_cast from 'struct B *' to 'struct E *' is not allowed}} (void)static_cast(*((B*)0)); // expected-error {{non-const reference to type 'struct E' cannot be initialized with a value of type 'struct B'}} // TODO: Test inaccessible base in context where it's accessible, i.e. // member function and friend. // TODO: Test DR427. This requires user-defined conversions, though. } // Enum conversions void t_529_7() { (void)static_cast(1); (void)static_cast(1.0); (void)static_cast(En1); // Bad code below (void)static_cast((int*)0); // expected-error {{static_cast from 'int *' to 'enum Enum' is not allowed}} } // Void pointer to object pointer void t_529_10() { (void)static_cast((void*)0); (void)static_cast((void*)0); // Bad code below (void)static_cast((const void*)0); // expected-error {{static_cast from 'void const *' to 'int *' casts away constness}} (void)static_cast((void*)0); // expected-error {{static_cast from 'void *' to 'void (*)(void)' is not allowed}} } // TODO: Test member pointers.