1 // RUN: %clang_cc1 -verify -std=c++11 %s 2 3 namespace N { 4 typedef char C; 5 } 6 7 namespace M { 8 typedef double D; 9 } 10 11 struct NonLiteral { NonLiteralNonLiteral12 NonLiteral() {} NonLiteralNonLiteral13 NonLiteral(int) {} operator intNonLiteral14 operator int() const { return 0; } 15 }; 16 struct Literal { LiteralLiteral17 constexpr Literal() {} operator intLiteral18 operator int() const { return 0; } 19 }; 20 21 struct S { 22 virtual int ImplicitlyVirtual() const; 23 }; 24 struct T {}; 25 26 template<typename T> struct ImplicitVirtualFromDependentBase : T { ImplicitlyVirtualImplicitVirtualFromDependentBase27 constexpr int ImplicitlyVirtual() const { return 0; } 28 }; 29 30 constexpr int a = ImplicitVirtualFromDependentBase<S>().ImplicitlyVirtual(); // expected-error {{constant expression}} expected-note {{cannot evaluate call to virtual function}} 31 constexpr int b = ImplicitVirtualFromDependentBase<T>().ImplicitlyVirtual(); // ok 32 constexpr int c = ImplicitVirtualFromDependentBase<S>().ImplicitVirtualFromDependentBase<S>::ImplicitlyVirtual(); // expected-error {{constant expression}} expected-note {{cannot evaluate call to virtual function}} 33 34 template<typename R> struct ConstexprMember { FConstexprMember35 constexpr R F() const { return 0; } 36 }; 37 constexpr int d = ConstexprMember<int>().F(); // ok 38 constexpr int e = ConstexprMember<NonLiteral>().F(); // expected-error {{constant expression}} expected-note {{non-literal type 'NonLiteral' cannot be used in a constant expression}} 39 40 template<typename ...P> struct ConstexprCtor { ConstexprCtorConstexprCtor41 constexpr ConstexprCtor(P...) {} 42 }; f1()43constexpr ConstexprCtor<> f1() { return {}; } // ok f2()44constexpr ConstexprCtor<int> f2() { return 0; } // ok f3()45constexpr ConstexprCtor<NonLiteral> f3() { return { 0 }; } // expected-error {{never produces a constant expression}} expected-note {{non-literal type 'NonLiteral}} f4()46constexpr ConstexprCtor<int, NonLiteral> f4() { return { 0, 0 }; } // expected-error {{never produces a constant expression}} expected-note {{non-literal type 'NonLiteral}} 47 48 struct VirtBase : virtual S {}; // expected-note {{here}} 49 50 namespace TemplateVBase { 51 template<typename T> struct T1 : virtual Literal { // expected-note {{here}} T1TemplateVBase::T152 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. T2TemplateVBase::T258 constexpr T2() {} 59 }; g2()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: T3()64 constexpr T3() {} 65 }; g3()66 constexpr T3<Literal> g3() { return {}; } // ok g4()67 constexpr T3<VirtBase> g4() { return {}; } // expected-error {{not a literal type}} 68 } 69