summaryrefslogtreecommitdiff
path: root/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p6.cpp
blob: 1a6dc9ecfb5d1998c0238c800343497cff6b7697 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// RUN: %clang_cc1 -verify -std=c++11 %s

namespace N {
  typedef char C;
}

namespace M {
  typedef double D;
}

struct NonLiteral {
  NonLiteral() {}
  NonLiteral(int) {} // expected-note 2{{here}}
  operator int() const { return 0; }
};
struct Literal {
  constexpr Literal() {}
  operator int() const { return 0; }
};

struct S {
  virtual int ImplicitlyVirtual() const;
};
struct T {};

template<typename T> struct ImplicitVirtualFromDependentBase : T {
  constexpr int ImplicitlyVirtual() { return 0; }
};

constexpr int a = ImplicitVirtualFromDependentBase<S>().ImplicitlyVirtual(); // expected-error {{constant expression}} expected-note {{cannot evaluate virtual function call}}
constexpr int b = ImplicitVirtualFromDependentBase<T>().ImplicitlyVirtual(); // ok
constexpr int c = ImplicitVirtualFromDependentBase<S>().ImplicitVirtualFromDependentBase<S>::ImplicitlyVirtual();

template<typename R> struct ConstexprMember {
  constexpr R F() { return 0; }
};
constexpr int d = ConstexprMember<int>().F(); // ok
constexpr int e = ConstexprMember<NonLiteral>().F(); // expected-error {{constant expression}}

template<typename ...P> struct ConstexprCtor {
  constexpr ConstexprCtor(P...) {}
};
constexpr ConstexprCtor<> f1() { return {}; } // ok
constexpr ConstexprCtor<int> f2() { return 0; } // ok
constexpr ConstexprCtor<NonLiteral> f3() { return { 0 }; } // expected-error {{never produces a constant expression}} expected-note {{non-constexpr constructor 'NonLiteral}}
constexpr ConstexprCtor<int, NonLiteral> f4() { return { 0, 0 }; } // expected-error {{never produces a constant expression}} expected-note {{non-constexpr constructor 'NonLiteral}}

struct VirtBase : virtual S {}; // expected-note {{here}}

namespace TemplateVBase {
  template<typename T> struct T1 : virtual Literal { // expected-note {{here}}
    constexpr T1() {} // expected-error {{constexpr constructor not allowed in struct with virtual base class}}
  };

  template<typename T> struct T2 : virtual T {
    // FIXME: This is ill-formed (no diagnostic required).
    // We should diagnose it now rather than waiting until instantiation.
    constexpr T2() {}
  };
  constexpr T2<Literal> g2() { return {}; }

  template<typename T> class T3 : public T { // expected-note {{class with virtual base class is not a literal type}}
  public:
    constexpr T3() {}
  };
  constexpr T3<Literal> g3() { return {}; } // ok
  constexpr T3<VirtBase> g4() { return {}; } // expected-error {{not a literal type}}
}