p2.cpp revision 4841ca5f83bf970f910ac7d154cdd71d2a3cf481
14677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger// RUN: %clang_cc1 -std=c++11 -verify %s
24677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
34677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingertemplate<int> struct X {};
44677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
54677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger// Constructor characteristics are:
64677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger//   - the template parameter list
74677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger//   - the parameter-type-list
84677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger//   - absence or presence of explicit
94677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger//   - absence or presence of constexpr
104677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct A {
114677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  A(X<0>) {} // expected-note 2{{here}}
124677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  constexpr A(X<1>) {}
134677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  explicit A(X<2>) {} // expected-note 3{{here}}
144677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  explicit constexpr A(X<3>) {} // expected-note 2{{here}}
154677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger};
164677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
174677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerA a0 { X<0>{} };
184677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerA a0i = { X<0>{} };
194677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr A a0c { X<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
204677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr A a0ic = { X<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
214677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
224677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerA a1 { X<1>{} };
234677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerA a1i = { X<1>{} };
244677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr A a1c { X<1>{} };
254677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr A a1ic = { X<1>{} };
264677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
274677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerA a2 { X<2>{} };
284677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerA a2i = { X<2>{} }; // expected-error {{constructor is explicit}}
294677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr A a2c { X<2>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
304677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr A a2ic = { X<2>{} }; // expected-error {{constructor is explicit}}
314677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
324677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerA a3 { X<3>{} };
334677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerA a3i = { X<3>{} }; // expected-error {{constructor is explicit}}
344677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr A a3c { X<3>{} };
354677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr A a3ic = { X<3>{} }; // expected-error {{constructor is explicit}}
364677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
374677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
384677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct B : A {
394677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  using A::A; // expected-note 7{{here}}
404677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger};
414677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
424677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerB b0 { X<0>{} };
434677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerB b0i = { X<0>{} };
444677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr B b0c { X<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
454677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr B b0ic = { X<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
464677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
474677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerB b1 { X<1>{} };
484677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerB b1i = { X<1>{} };
494677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr B b1c { X<1>{} };
504677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr B b1ic = { X<1>{} };
514677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
52ae665a522bd46bea44c5ea84c89c8b1731954170Stephen HemmingerB b2 { X<2>{} };
534677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerB b2i = { X<2>{} }; // expected-error {{constructor is explicit}}
544677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr B b2c { X<2>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
55f5f1a6c5836a405381fb9d4be74d5ddf98404db1osdl.net!shemmingerconstexpr B b2ic = { X<2>{} }; // expected-error {{constructor is explicit}}
56f5f1a6c5836a405381fb9d4be74d5ddf98404db1osdl.net!shemminger
57ae665a522bd46bea44c5ea84c89c8b1731954170Stephen HemmingerB b3 { X<3>{} };
587339c0bb74e7e55ad70c29ced5628f26dd821900shemmingerB b3i = { X<3>{} }; // expected-error {{constructor is explicit}}
59f5f1a6c5836a405381fb9d4be74d5ddf98404db1osdl.net!shemmingerconstexpr B b3c { X<3>{} };
60f5f1a6c5836a405381fb9d4be74d5ddf98404db1osdl.net!shemmingerconstexpr B b3ic = { X<3>{} }; // expected-error {{constructor is explicit}}
61f5f1a6c5836a405381fb9d4be74d5ddf98404db1osdl.net!shemminger
624677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
634677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger// 'constexpr' is OK even if the constructor doesn't obey the constraints.
644677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct NonLiteral { NonLiteral(); };
654677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct NonConstexpr { NonConstexpr(); constexpr NonConstexpr(int); }; // expected-note {{here}}
66ae665a522bd46bea44c5ea84c89c8b1731954170Stephen Hemmingerstruct Constexpr { constexpr Constexpr(int) {} };
67ae665a522bd46bea44c5ea84c89c8b1731954170Stephen Hemminger
684677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct BothNonLiteral : NonLiteral, Constexpr { using Constexpr::Constexpr; }; // expected-note {{base class 'NonLiteral' of non-literal type}}
694677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr BothNonLiteral bothNL{42}; // expected-error {{constexpr variable cannot have non-literal type 'const BothNonLiteral'}}
704677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
714677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct BothNonConstexpr : NonConstexpr, Constexpr { using Constexpr::Constexpr; }; // expected-note {{non-constexpr constructor 'NonConstexpr}}
724677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr BothNonConstexpr bothNC{42}; // expected-error {{must be initialized by a constant expression}} expected-note {{in call to 'BothNonConstexpr(42)'}}
734677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
744677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
754677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct ConstexprEval {
764677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  constexpr ConstexprEval(int a, const char *p) : k(p[a]) {}
774677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  char k;
784677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger};
794677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct ConstexprEval2 {
804677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  char k2 = 'x';
814677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger};
824677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct ConstexprEval3 : ConstexprEval, ConstexprEval2 {
834677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  using ConstexprEval::ConstexprEval;
844677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger};
854677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr ConstexprEval3 ce{4, "foobar"};
864677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstatic_assert(ce.k == 'a', "");
874677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstatic_assert(ce.k2 == 'x', "");
884677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
894677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
904677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct TemplateCtors {
914677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  constexpr TemplateCtors() {}
924677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  template<template<int> class T> TemplateCtors(X<0>, T<0>);
934677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  template<int N> TemplateCtors(X<1>, X<N>);
944677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  template<typename T> TemplateCtors(X<2>, T);
954677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
964677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  template<typename T = int> TemplateCtors(int, int = 0, int = 0); // expected-note {{inherited from here}}
974677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger};
984677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
994677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerstruct UsingTemplateCtors : TemplateCtors {
1004677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  using TemplateCtors::TemplateCtors; // expected-note 4{{here}} expected-note {{candidate}}
101ae665a522bd46bea44c5ea84c89c8b1731954170Stephen Hemminger
1024677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  constexpr UsingTemplateCtors(X<0>, X<0>) {}
1034677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  constexpr UsingTemplateCtors(X<1>, X<1>) {}
1044677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  constexpr UsingTemplateCtors(X<2>, X<2>) {}
1054677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
1064677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  template<int = 0> constexpr UsingTemplateCtors(int) {} // expected-note {{candidate}}
1074677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  template<typename T = void> constexpr UsingTemplateCtors(int, int) {}
1084677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger  template<typename T, typename U> constexpr UsingTemplateCtors(int, int, int) {}
1094677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger};
1104677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
1114677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingertemplate<int> struct Y {};
1124677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr UsingTemplateCtors uct1{ X<0>{}, X<0>{} };
1134677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr UsingTemplateCtors uct2{ X<0>{}, Y<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
1144677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr UsingTemplateCtors uct3{ X<1>{}, X<0>{} }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
1154677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr UsingTemplateCtors uct4{ X<1>{}, X<1>{} };
1164677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr UsingTemplateCtors uct5{ X<2>{}, 0 }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
1174677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr UsingTemplateCtors uct6{ X<2>{}, X<2>{} };
1184677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger
1194677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr UsingTemplateCtors utc7{ 0 }; // expected-error {{ambiguous}}
1204677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr UsingTemplateCtors utc8{ 0, 0 }; // ok
1214677a549831336400e16210e6f12251dc07d5232osdl.net!shemmingerconstexpr UsingTemplateCtors utc9{ 0, 0, 0 }; // expected-error {{must be initialized by a constant expression}} expected-note {{non-constexpr}}
1224677a549831336400e16210e6f12251dc07d5232osdl.net!shemminger