xref: /aosp_15_r20/external/clang/test/CXX/special/class.copy/p13-0x.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2*67e74705SXin Li 
3*67e74705SXin Li // If the implicitly-defined constructor would satisfy the requirements of a
4*67e74705SXin Li // constexpr constructor, the implicitly-defined constructor is constexpr.
5*67e74705SXin Li struct Constexpr1 {
Constexpr1Constexpr16*67e74705SXin Li   constexpr Constexpr1() : n(0) {}
7*67e74705SXin Li   int n;
8*67e74705SXin Li };
9*67e74705SXin Li constexpr Constexpr1 c1a = Constexpr1(Constexpr1()); // ok
10*67e74705SXin Li constexpr Constexpr1 c1b = Constexpr1(Constexpr1(c1a)); // ok
11*67e74705SXin Li 
12*67e74705SXin Li struct Constexpr2 {
13*67e74705SXin Li   Constexpr1 ce1;
14*67e74705SXin Li   constexpr Constexpr2() = default;
Constexpr2Constexpr215*67e74705SXin Li   constexpr Constexpr2(const Constexpr2 &o) : ce1(o.ce1) {}
16*67e74705SXin Li   // no move constructor
17*67e74705SXin Li };
18*67e74705SXin Li 
19*67e74705SXin Li constexpr Constexpr2 c2a = Constexpr2(Constexpr2()); // ok
20*67e74705SXin Li constexpr Constexpr2 c2b = Constexpr2(Constexpr2(c2a)); // ok
21*67e74705SXin Li 
22*67e74705SXin Li struct Constexpr3 {
23*67e74705SXin Li   Constexpr2 ce2;
24*67e74705SXin Li   // all special constructors are constexpr, move ctor calls ce2's copy ctor
25*67e74705SXin Li };
26*67e74705SXin Li 
27*67e74705SXin Li constexpr Constexpr3 c3a = Constexpr3(Constexpr3()); // ok
28*67e74705SXin Li constexpr Constexpr3 c3b = Constexpr3(Constexpr3(c3a)); // ok
29*67e74705SXin Li 
30*67e74705SXin Li struct NonConstexprCopy {
31*67e74705SXin Li   constexpr NonConstexprCopy() = default;
32*67e74705SXin Li   NonConstexprCopy(const NonConstexprCopy &);
33*67e74705SXin Li   constexpr NonConstexprCopy(NonConstexprCopy &&) = default;
34*67e74705SXin Li 
35*67e74705SXin Li   int n = 42;
36*67e74705SXin Li };
37*67e74705SXin Li 
38*67e74705SXin Li NonConstexprCopy::NonConstexprCopy(const NonConstexprCopy &) = default; // expected-note {{here}}
39*67e74705SXin Li 
40*67e74705SXin Li constexpr NonConstexprCopy ncc1 = NonConstexprCopy(NonConstexprCopy()); // ok
41*67e74705SXin Li constexpr NonConstexprCopy ncc2 = ncc1; // expected-error {{constant expression}} expected-note {{non-constexpr constructor}}
42*67e74705SXin Li 
43*67e74705SXin Li struct NonConstexprDefault {
44*67e74705SXin Li   NonConstexprDefault() = default;
NonConstexprDefaultNonConstexprDefault45*67e74705SXin Li   constexpr NonConstexprDefault(int n) : n(n) {}
46*67e74705SXin Li   int n;
47*67e74705SXin Li };
48*67e74705SXin Li struct Constexpr4 {
49*67e74705SXin Li   NonConstexprDefault ncd;
50*67e74705SXin Li };
51*67e74705SXin Li 
52*67e74705SXin Li constexpr NonConstexprDefault ncd = NonConstexprDefault(NonConstexprDefault(1));
53*67e74705SXin Li constexpr Constexpr4 c4a = { ncd };
54*67e74705SXin Li constexpr Constexpr4 c4b = Constexpr4(c4a);
55*67e74705SXin Li constexpr Constexpr4 c4c = Constexpr4(static_cast<Constexpr4&&>(const_cast<Constexpr4&>(c4b)));
56*67e74705SXin Li 
57*67e74705SXin Li struct Constexpr5Base {};
Constexpr5Constexpr558*67e74705SXin Li struct Constexpr5 : Constexpr5Base { constexpr Constexpr5() {} };
59*67e74705SXin Li constexpr Constexpr5 ce5move = Constexpr5();
60*67e74705SXin Li constexpr Constexpr5 ce5copy = ce5move;
61*67e74705SXin Li 
62*67e74705SXin Li // An explicitly-defaulted constructor doesn't become constexpr until the end of
63*67e74705SXin Li // its class. Make sure we note that the class has a constexpr constructor when
64*67e74705SXin Li // that happens.
65*67e74705SXin Li namespace PR13052 {
66*67e74705SXin Li   template<typename T> struct S {
67*67e74705SXin Li     S() = default; // expected-note 2{{here}}
68*67e74705SXin Li     S(S&&) = default;
69*67e74705SXin Li     S(const S&) = default;
70*67e74705SXin Li     T t;
71*67e74705SXin Li   };
72*67e74705SXin Li 
73*67e74705SXin Li   struct U {
74*67e74705SXin Li     U() = default;
75*67e74705SXin Li     U(U&&) = default;
76*67e74705SXin Li     U(const U&) = default;
77*67e74705SXin Li   };
78*67e74705SXin Li 
79*67e74705SXin Li   struct V {
80*67e74705SXin Li     V(); // expected-note {{here}}
81*67e74705SXin Li     V(V&&) = default;
82*67e74705SXin Li     V(const V&) = default;
83*67e74705SXin Li   };
84*67e74705SXin Li 
85*67e74705SXin Li   struct W {
86*67e74705SXin Li     W(); // expected-note {{here}}
87*67e74705SXin Li   };
88*67e74705SXin Li 
89*67e74705SXin Li   static_assert(__is_literal_type(U), "");
90*67e74705SXin Li   static_assert(!__is_literal_type(V), "");
91*67e74705SXin Li   static_assert(!__is_literal_type(W), "");
92*67e74705SXin Li   static_assert(__is_literal_type(S<U>), "");
93*67e74705SXin Li   static_assert(!__is_literal_type(S<V>), "");
94*67e74705SXin Li   static_assert(!__is_literal_type(S<W>), "");
95*67e74705SXin Li 
96*67e74705SXin Li   struct X {
97*67e74705SXin Li     friend constexpr U::U() noexcept;
98*67e74705SXin Li     friend constexpr U::U(U&&) noexcept;
99*67e74705SXin Li     friend constexpr U::U(const U&) noexcept;
100*67e74705SXin Li     friend constexpr V::V(); // expected-error {{follows non-constexpr declaration}}
101*67e74705SXin Li     friend constexpr V::V(V&&) noexcept;
102*67e74705SXin Li     friend constexpr V::V(const V&) noexcept;
103*67e74705SXin Li     friend constexpr W::W(); // expected-error {{follows non-constexpr declaration}}
104*67e74705SXin Li     friend constexpr W::W(W&&) noexcept;
105*67e74705SXin Li     friend constexpr W::W(const W&) noexcept;
106*67e74705SXin Li     friend constexpr S<U>::S() noexcept;
107*67e74705SXin Li     friend constexpr S<U>::S(S<U>&&) noexcept;
108*67e74705SXin Li     friend constexpr S<U>::S(const S<U>&) noexcept;
109*67e74705SXin Li     friend constexpr S<V>::S(); // expected-error {{follows non-constexpr declaration}}
110*67e74705SXin Li     friend constexpr S<V>::S(S<V>&&) noexcept;
111*67e74705SXin Li     friend constexpr S<V>::S(const S<V>&) noexcept;
112*67e74705SXin Li     friend constexpr S<W>::S(); // expected-error {{follows non-constexpr declaration}}
113*67e74705SXin Li     friend constexpr S<W>::S(S<W>&&) noexcept;
114*67e74705SXin Li     friend constexpr S<W>::S(const S<W>&) noexcept;
115*67e74705SXin Li   };
116*67e74705SXin Li }
117*67e74705SXin Li 
118*67e74705SXin Li namespace Mutable {
119*67e74705SXin Li   struct A {
120*67e74705SXin Li     constexpr A(A &);
121*67e74705SXin Li     A(const A &);
122*67e74705SXin Li   };
123*67e74705SXin Li   struct B {
124*67e74705SXin Li     constexpr B(const B &) = default; // ok
125*67e74705SXin Li     mutable A a;
126*67e74705SXin Li   };
127*67e74705SXin Li   struct C {
128*67e74705SXin Li     constexpr C(const C &) = default; // expected-error {{not constexpr}}
129*67e74705SXin Li     A a;
130*67e74705SXin Li   };
131*67e74705SXin Li }
132