p6.cpp revision 840462670ba7a6bc26265a2306b35f2f0f01f51c
1// RUN: %clang_cc1 -verify -std=c++11 %s 2 3namespace N { 4 typedef char C; 5} 6 7namespace M { 8 typedef double D; 9} 10 11struct NonLiteral { 12 NonLiteral() {} 13 NonLiteral(int) {} // expected-note 2{{here}} 14 operator int() const { return 0; } 15}; 16struct Literal { 17 constexpr Literal() {} 18 operator int() const { return 0; } 19}; 20 21struct S { 22 virtual int ImplicitlyVirtual() const; 23}; 24struct T {}; 25 26template<typename T> struct ImplicitVirtualFromDependentBase : T { 27 constexpr int ImplicitlyVirtual() const { return 0; } 28}; 29 30constexpr int a = ImplicitVirtualFromDependentBase<S>().ImplicitlyVirtual(); // expected-error {{constant expression}} expected-note {{cannot evaluate virtual function call}} 31constexpr int b = ImplicitVirtualFromDependentBase<T>().ImplicitlyVirtual(); // ok 32constexpr int c = ImplicitVirtualFromDependentBase<S>().ImplicitVirtualFromDependentBase<S>::ImplicitlyVirtual(); 33 34template<typename R> struct ConstexprMember { 35 constexpr R F() const { return 0; } 36}; 37constexpr int d = ConstexprMember<int>().F(); // ok 38constexpr int e = ConstexprMember<NonLiteral>().F(); // expected-error {{constant expression}} 39 40template<typename ...P> struct ConstexprCtor { 41 constexpr ConstexprCtor(P...) {} 42}; 43constexpr ConstexprCtor<> f1() { return {}; } // ok 44constexpr ConstexprCtor<int> f2() { return 0; } // ok 45constexpr ConstexprCtor<NonLiteral> f3() { return { 0 }; } // expected-error {{never produces a constant expression}} expected-note {{non-constexpr constructor 'NonLiteral}} 46constexpr ConstexprCtor<int, NonLiteral> f4() { return { 0, 0 }; } // expected-error {{never produces a constant expression}} expected-note {{non-constexpr constructor 'NonLiteral}} 47 48struct VirtBase : virtual S {}; // expected-note {{here}} 49 50namespace TemplateVBase { 51 template<typename T> struct T1 : virtual Literal { // expected-note {{here}} 52 constexpr T1() {} // expected-error {{constexpr constructor not allowed in struct with virtual base class}} 53 }; 54 55 template<typename T> struct T2 : virtual T { 56 // FIXME: This is ill-formed (no diagnostic required). 57 // We should diagnose it now rather than waiting until instantiation. 58 constexpr T2() {} 59 }; 60 constexpr T2<Literal> g2() { return {}; } 61 62 template<typename T> class T3 : public T { // expected-note {{class with virtual base class is not a literal type}} 63 public: 64 constexpr T3() {} 65 }; 66 constexpr T3<Literal> g3() { return {}; } // ok 67 constexpr T3<VirtBase> g4() { return {}; } // expected-error {{not a literal type}} 68} 69