17002f4c03c2d0544f4e8bea8d3a5636519081e35John McCall// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -fcxx-exceptions %s
248270a36749b7f10677a514a03bbd2cf90697d58John McCall
348270a36749b7f10677a514a03bbd2cf90697d58John McCall// An explicitly-defaulted function may be declared constexpr only if it would
448270a36749b7f10677a514a03bbd2cf90697d58John McCall// have been implicitly declared as constexpr.
548270a36749b7f10677a514a03bbd2cf90697d58John McCallstruct S1 {
648270a36749b7f10677a514a03bbd2cf90697d58John McCall  constexpr S1() = default; // expected-error {{defaulted definition of default constructor is not constexpr}}
7cc5306ac8a39cdd5e7f83e597cba911e97c4a595Fariborz Jahanian  constexpr S1(const S1&) = default;
848270a36749b7f10677a514a03bbd2cf90697d58John McCall  constexpr S1(S1&&) = default;
948270a36749b7f10677a514a03bbd2cf90697d58John McCall  constexpr S1 &operator=(const S1&) const = default; // expected-error {{explicitly-defaulted copy assignment operator may not have}}
107ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  constexpr S1 &operator=(S1&&) const = default; // expected-error {{explicitly-defaulted move assignment operator may not have}}
1148270a36749b7f10677a514a03bbd2cf90697d58John McCall  constexpr ~S1() = default; // expected-error {{destructor cannot be marked constexpr}}
1248270a36749b7f10677a514a03bbd2cf90697d58John McCall  int n;
1348270a36749b7f10677a514a03bbd2cf90697d58John McCall};
147ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstruct NoCopyMove {
15a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor  constexpr NoCopyMove() {}
16a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor  NoCopyMove(const NoCopyMove&);
1748270a36749b7f10677a514a03bbd2cf90697d58John McCall  NoCopyMove(NoCopyMove&&);
1848270a36749b7f10677a514a03bbd2cf90697d58John McCall};
1948270a36749b7f10677a514a03bbd2cf90697d58John McCallstruct S2 {
2048270a36749b7f10677a514a03bbd2cf90697d58John McCall  constexpr S2() = default;
2148270a36749b7f10677a514a03bbd2cf90697d58John McCall  constexpr S2(const S2&) = default; // expected-error {{defaulted definition of copy constructor is not constexpr}}
2248270a36749b7f10677a514a03bbd2cf90697d58John McCall  constexpr S2(S2&&) = default; // expected-error {{defaulted definition of move constructor is not constexpr}}
2348270a36749b7f10677a514a03bbd2cf90697d58John McCall  NoCopyMove ncm;
2448270a36749b7f10677a514a03bbd2cf90697d58John McCall};
2548270a36749b7f10677a514a03bbd2cf90697d58John McCall
267ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall// If a function is explicitly defaulted on its first declaration
2748270a36749b7f10677a514a03bbd2cf90697d58John McCall//   -- it is implicitly considered to be constexpr if the implicit declaration
2848270a36749b7f10677a514a03bbd2cf90697d58John McCall//      would be
2948270a36749b7f10677a514a03bbd2cf90697d58John McCallstruct S3 {
3048270a36749b7f10677a514a03bbd2cf90697d58John McCall  S3() = default;
3148270a36749b7f10677a514a03bbd2cf90697d58John McCall  S3(const S3&) = default;
3248270a36749b7f10677a514a03bbd2cf90697d58John McCall  S3(S3&&) = default;
3348270a36749b7f10677a514a03bbd2cf90697d58John McCall  constexpr S3(int n) : n(n) {}
3448270a36749b7f10677a514a03bbd2cf90697d58John McCall  int n;
3548270a36749b7f10677a514a03bbd2cf90697d58John McCall};
3648270a36749b7f10677a514a03bbd2cf90697d58John McCallconstexpr S3 s3a = S3(0);
3748270a36749b7f10677a514a03bbd2cf90697d58John McCallconstexpr S3 s3b = s3a;
3848270a36749b7f10677a514a03bbd2cf90697d58John McCallconstexpr S3 s3c = S3();
3948270a36749b7f10677a514a03bbd2cf90697d58John McCallconstexpr S3 s3d; // expected-error {{default initialization of an object of const type 'const S3' requires a user-provided default constructor}}
4048270a36749b7f10677a514a03bbd2cf90697d58John McCall
417ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstruct S4 {
422fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu  S4() = default;
4348270a36749b7f10677a514a03bbd2cf90697d58John McCall  S4(const S4&) = default; // expected-note {{here}}
4448270a36749b7f10677a514a03bbd2cf90697d58John McCall  S4(S4&&) = default; // expected-note {{here}}
4548270a36749b7f10677a514a03bbd2cf90697d58John McCall  NoCopyMove ncm;
467ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall};
477ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallconstexpr S4 s4a{}; // ok
487ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallconstexpr S4 s4b = S4(); // expected-error {{constant expression}} expected-note {{non-constexpr constructor}}
497ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallconstexpr S4 s4c = s4a; // expected-error {{constant expression}} expected-note {{non-constexpr constructor}}
507ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
517ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstruct S5 {
527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  constexpr S5();
537ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  int n = 1, m = n + 3;
547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall};
557ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallconstexpr S5::S5() = default;
567ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstatic_assert(S5().m == 4, "");
577ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
587ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall
597ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall// An explicitly-defaulted function may have an exception specification only if
607ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall// it is compatible with the exception specification on an implicit declaration.
617ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallstruct E1 {
627ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  E1() noexcept = default;
637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  E1(const E1&) noexcept = default;
647ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  E1(E1&&) noexcept = default;
657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  E1 &operator=(const E1&) noexcept = default;
667ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  E1 &operator=(E1&&) noexcept = default;
677ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  ~E1() noexcept = default;
687ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall};
6932daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCallstruct E2 {
7032daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E2() noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}}
7132daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E2(const E2&) noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted copy constructor does not match the calculated one}}
7232daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E2(E2&&) noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted move constructor does not match the calculated one}}
7332daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E2 &operator=(const E2&) noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted copy assignment operator does not match the calculated one}}
7432daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E2 &operator=(E2&&) noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted move assignment operator does not match the calculated one}}
7532daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  ~E2() noexcept(false) = default; // expected-error {{exception specification of explicitly defaulted destructor does not match the calculated one}}
7632daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall};
7732daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall
7832daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall// If a function is explicitly defaulted on its first declaration
792fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu//   -- it is implicitly considered to have the same exception-specification as
8032daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall//      if it had been implicitly declared
8132daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCallstruct E3 {
8232daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E3() = default;
8332daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E3(const E3&) = default;
8432daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E3(E3&&) = default;
8532daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E3 &operator=(const E3&) = default;
8632daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E3 &operator=(E3&&) = default;
8732daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  ~E3() = default;
8832daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall};
8932daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCallE3 e3;
9032daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCallstatic_assert(noexcept(E3(), E3(E3()), E3(e3), e3 = E3(), e3 = e3), "");
9132daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCallstruct E4 {
92d880f52be3e4a8ccad92ac31932eeae5e0870a93Douglas Gregor  E4() noexcept(false);
9332daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E4(const E4&) noexcept(false);
9432daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E4(E4&&) noexcept(false);
9532daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E4 &operator=(const E4&) noexcept(false);
9632daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E4 &operator=(E4&&) noexcept(false);
9732daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  ~E4() noexcept(false);
9832daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall};
9932daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCallstruct E5 {
10032daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E5() = default;
10132daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E5(const E5&) = default;
10232daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E5(E5&&) = default;
10332daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E5 &operator=(const E5&) = default;
10432daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E5 &operator=(E5&&) = default;
10532daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  ~E5() = default;
10632daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall
10732daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall  E4 e4;
10832daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall};
10932daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCallE5 e5;
11032daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCallstatic_assert(!noexcept(E5()), "");
111static_assert(!noexcept(E5(static_cast<E5&&>(e5))), "");
112static_assert(!noexcept(E5(e5)), "");
113static_assert(!noexcept(e5 = E5()), "");
114static_assert(!noexcept(e5 = e5), "");
115
116namespace PR13492 {
117  struct B {
118    B() = default;
119  };
120
121  void f() {
122    const B b; // expected-error {{default initialization of an object of const type 'const PR13492::B' requires a user-provided default constructor}}
123  }
124}
125