Home | History | Annotate | Download | only in dcl.constexpr
      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 {
     12   NonLiteral() {}
     13   NonLiteral(int) {} // expected-note 2{{here}}
     14   operator int() const { return 0; }
     15 };
     16 struct Literal {
     17   constexpr Literal() {}
     18   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 {
     27   constexpr int ImplicitlyVirtual() const { return 0; }
     28 };
     29 
     30 constexpr int a = ImplicitVirtualFromDependentBase<S>().ImplicitlyVirtual(); // expected-error {{constant expression}} expected-note {{cannot evaluate virtual function call}}
     31 constexpr int b = ImplicitVirtualFromDependentBase<T>().ImplicitlyVirtual(); // ok
     32 constexpr int c = ImplicitVirtualFromDependentBase<S>().ImplicitVirtualFromDependentBase<S>::ImplicitlyVirtual();
     33 
     34 template<typename R> struct ConstexprMember {
     35   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}}
     39 
     40 template<typename ...P> struct ConstexprCtor {
     41   constexpr ConstexprCtor(P...) {}
     42 };
     43 constexpr ConstexprCtor<> f1() { return {}; } // ok
     44 constexpr ConstexprCtor<int> f2() { return 0; } // ok
     45 constexpr ConstexprCtor<NonLiteral> f3() { return { 0 }; } // expected-error {{never produces a constant expression}} expected-note {{non-constexpr constructor 'NonLiteral}}
     46 constexpr ConstexprCtor<int, NonLiteral> f4() { return { 0, 0 }; } // expected-error {{never produces a constant expression}} expected-note {{non-constexpr constructor '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}}
     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