• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -std=c++11 -verify %s
2 //
3 // Note: [class.inhctor] was removed by P0136R1. This tests the new behavior
4 // for the wording that used to be there.
5 
6 template<int> struct X {};
7 
8 // Constructor characteristics are:
9 //   - the template parameter list
10 //   - the parameter-type-list
11 //   - absence or presence of explicit
12 //   - absence or presence of constexpr
13 struct A {
AA14   A(X<0>) {} // expected-note 4{{here}}
AA15   constexpr A(X<1>) {}
AA16   explicit A(X<2>) {} // expected-note 6{{here}}
AA17   explicit constexpr A(X<3>) {} // expected-note 4{{here}}
18 };
19 
20 A a0 { X<0>{} };
21 A a0i = { X<0>{} };
22 constexpr A a0c { X<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
23 constexpr A a0ic = { X<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
24 
25 A a1 { X<1>{} };
26 A a1i = { X<1>{} };
27 constexpr A a1c { X<1>{} };
28 constexpr A a1ic = { X<1>{} };
29 
30 A a2 { X<2>{} };
31 A a2i = { X<2>{} }; // expected-error {{constructor is explicit}}
32 constexpr A a2c { X<2>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
33 constexpr A a2ic = { X<2>{} }; // expected-error {{constructor is explicit}}
34 
35 A a3 { X<3>{} };
36 A a3i = { X<3>{} }; // expected-error {{constructor is explicit}}
37 constexpr A a3c { X<3>{} };
38 constexpr A a3ic = { X<3>{} }; // expected-error {{constructor is explicit}}
39 
40 
41 struct B : A {
42   using A::A;
43 };
44 
45 B b0 { X<0>{} };
46 B b0i = { X<0>{} };
47 constexpr B b0c { X<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
48 constexpr B b0ic = { X<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
49 
50 B b1 { X<1>{} };
51 B b1i = { X<1>{} };
52 constexpr B b1c { X<1>{} };
53 constexpr B b1ic = { X<1>{} };
54 
55 B b2 { X<2>{} };
56 B b2i = { X<2>{} }; // expected-error {{constructor is explicit}}
57 constexpr B b2c { X<2>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
58 constexpr B b2ic = { X<2>{} }; // expected-error {{constructor is explicit}}
59 
60 B b3 { X<3>{} };
61 B b3i = { X<3>{} }; // expected-error {{constructor is explicit}}
62 constexpr B b3c { X<3>{} };
63 constexpr B b3ic = { X<3>{} }; // expected-error {{constructor is explicit}}
64 
65 
66 // 'constexpr' is OK even if the constructor doesn't obey the constraints.
67 struct NonLiteral { NonLiteral(); };
68 struct NonConstexpr { NonConstexpr(); constexpr NonConstexpr(int); };
ConstexprConstexpr69 struct Constexpr { constexpr Constexpr(int) {} };
70 
71 struct BothNonLiteral : NonLiteral, Constexpr { using Constexpr::Constexpr; }; // expected-note {{base class 'NonLiteral' of non-literal type}}
72 constexpr BothNonLiteral bothNL{42}; // expected-error {{constexpr variable cannot have non-literal type 'const BothNonLiteral'}}
73 
74 // FIXME: This diagnostic is not very good. We should explain that the problem is that base class NonConstexpr cannot be initialized.
75 struct BothNonConstexpr
76     : NonConstexpr,
77       Constexpr {
78   using Constexpr::Constexpr; // expected-note {{here}}
79 };
80 constexpr BothNonConstexpr bothNC{42}; // expected-error {{must be initialized by a constant expression}} expected-note {{inherited from base class 'Constexpr'}}
81 
82 
83 struct ConstexprEval {
ConstexprEvalConstexprEval84   constexpr ConstexprEval(int a, const char *p) : k(p[a]) {}
85   char k;
86 };
87 struct ConstexprEval2 {
88   char k2 = 'x';
89 };
90 struct ConstexprEval3 : ConstexprEval, ConstexprEval2 {
91   using ConstexprEval::ConstexprEval;
92 };
93 constexpr ConstexprEval3 ce{4, "foobar"};
94 static_assert(ce.k == 'a', "");
95 static_assert(ce.k2 == 'x', "");
96 
97 
98 struct TemplateCtors { // expected-note 2{{candidate constructor (the implicit}}
TemplateCtorsTemplateCtors99   constexpr TemplateCtors() {} // expected-note {{candidate inherited constructor}}
100   template<template<int> class T> TemplateCtors(X<0>, T<0>); // expected-note {{here}} expected-note {{candidate inherited constructor}}
101   template<int N> TemplateCtors(X<1>, X<N>); // expected-note {{here}} expected-note {{candidate inherited constructor}}
102   template<typename T> TemplateCtors(X<2>, T); // expected-note {{here}} expected-note {{candidate inherited constructor}}
103 
104   template<typename T = int> TemplateCtors(int, int = 0, int = 0);
105 };
106 
107 struct UsingTemplateCtors : TemplateCtors { // expected-note 2{{candidate constructor (the implicit}}
108   using TemplateCtors::TemplateCtors; // expected-note 6{{inherited here}}
109 
UsingTemplateCtorsUsingTemplateCtors110   constexpr UsingTemplateCtors(X<0>, X<0>) {} // expected-note {{not viable}}
UsingTemplateCtorsUsingTemplateCtors111   constexpr UsingTemplateCtors(X<1>, X<1>) {} // expected-note {{not viable}}
UsingTemplateCtorsUsingTemplateCtors112   constexpr UsingTemplateCtors(X<2>, X<2>) {} // expected-note {{not viable}}
113 
UsingTemplateCtorsUsingTemplateCtors114   template<int = 0> constexpr UsingTemplateCtors(int) {} // expected-note {{not viable}}
UsingTemplateCtorsUsingTemplateCtors115   template<typename T = void> constexpr UsingTemplateCtors(int, int) {} // expected-note {{not viable}}
UsingTemplateCtorsUsingTemplateCtors116   template<typename T, typename U> constexpr UsingTemplateCtors(int, int, int) {} // expected-note {{couldn't infer}}
117 };
118 
119 template<int> struct Y {};
120 constexpr UsingTemplateCtors uct1{ X<0>{}, X<0>{} };
121 constexpr UsingTemplateCtors uct2{ X<0>{}, Y<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
122 constexpr UsingTemplateCtors uct3{ X<1>{}, X<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
123 constexpr UsingTemplateCtors uct4{ X<1>{}, X<1>{} };
124 constexpr UsingTemplateCtors uct5{ X<2>{}, 0 }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
125 constexpr UsingTemplateCtors uct6{ X<2>{}, X<2>{} };
126 
127 constexpr UsingTemplateCtors utc7{ 0 }; // ok
128 constexpr UsingTemplateCtors utc8{ 0, 0 }; // ok
129 // FIXME: The standard says that UsingTemplateCtors' (int, int, int) constructor
130 // hides the one from TemplateCtors, even though the template parameter lists
131 // don't match. It's not clear that that's *really* the intent, and it's not
132 // what other compilers do.
133 constexpr UsingTemplateCtors utc9{ 0, 0, 0 }; // expected-error {{no matching constructor}}
134