1 // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall -Wno-unused-local-typedefs %s
3 template<bool b> struct ExceptionIf { static int f(); };
4 template<> struct ExceptionIf<false> { typedef int f; };
6 // The exception specification of a defaulted default constructor depends on
7 // the contents of in-class member initializers. However, the in-class member
8 // initializers can depend on the exception specification of the constructor,
9 // since the class is considered complete within them. We reject any such cases.
10 namespace InClassInitializers {
11 // Noexcept::Noexcept() is implicitly declared as noexcept(false), because it
12 // directly invokes ThrowSomething(). However...
14 // If noexcept(Noexcept()) is false, then Noexcept() is a constant expression,
15 // so noexcept(Noexcept()) is true. But if noexcept(Noexcept()) is true, then
16 // Noexcept::Noexcept is not declared constexpr, therefore noexcept(Noexcept())
18 bool ThrowSomething() noexcept(false);
20 bool b = // expected-note {{declared here}}
21 noexcept(ConstExpr()) && ThrowSomething(); // expected-error {{default member initializer for 'b' needed}}
24 // Much more obviously broken: we can't parse the initializer without already
25 // knowing whether it produces a noexcept expression.
27 int n = // expected-note {{declared here}}
28 ExceptionIf<noexcept(TemplateArg())>::f(); // expected-error {{default member initializer for 'n' needed}}
31 // And within a nested class.
34 int n = // expected-note {{declared here}}
35 ExceptionIf<noexcept(Nested())>::f();
36 } inner; // expected-error {{default member initializer for 'n' needed}}
41 int n = Inner().n; // expected-error {{initializer for 'n' needed}}
43 int n = ExceptionIf<noexcept(Nested2())>::f(); // expected-note {{declared here}}
48 namespace ExceptionSpecification {
49 // FIXME: This diagnostic is quite useless; we should indicate whose
50 // exception specification we were looking for and why.
53 T() noexcept(!noexcept(Nested()));
54 } t; // expected-error{{exception specification is not available until end of class definition}}
58 namespace DefaultArgument {
61 T(int = ExceptionIf<noexcept(Default())::f()); // expected-error {{call to implicitly-deleted default constructor}}
62 } t; // expected-note {{has no default constructor}}
66 namespace ImplicitDtorExceptionSpec {
77 virtual ~B() {} // expected-note {{here}}
86 ~D(); // expected-error {{more lax than base}}
96 struct nothrow_t {} nothrow;
97 void *operator new(decltype(sizeof(0)), nothrow_t) noexcept;
99 namespace PotentiallyConstructed {
100 template<bool NE> struct A {
102 A(const A&) noexcept(NE);
104 A &operator=(const A&) noexcept(NE);
105 A &operator=(A&&) noexcept(NE);
109 template<bool NE> struct B : virtual A<NE> {};
111 template<bool NE> struct C : virtual A<NE> {
112 virtual void f() = 0; // expected-note 2{{unimplemented}}
115 template<bool NE> struct D final : C<NE> {
119 template<typename T, bool A, bool B, bool C, bool D, bool E, bool F> void check() {
122 static_assert(noexcept(a = a) == D, "");
123 static_assert(noexcept(a = static_cast<T&&>(a)) == E, "");
124 static_assert(noexcept(delete &a) == F, ""); // expected-warning 2{{abstract}}
126 // These are last because the first failure here causes instantiation to bail out.
127 static_assert(noexcept(new (nothrow) T()) == A, ""); // expected-error 2{{abstract}}
128 static_assert(noexcept(new (nothrow) T(a)) == B, "");
129 static_assert(noexcept(new (nothrow) T(static_cast<T&&>(a))) == C, "");
132 template void check<A<false>, 0, 0, 0, 0, 0, 0>();
133 template void check<A<true >, 1, 1, 1, 1, 1, 1>();
134 template void check<B<false>, 0, 0, 0, 0, 0, 0>();
135 template void check<B<true >, 1, 1, 1, 1, 1, 1>();
136 template void check<C<false>, 1, 1, 1, 0, 0, 0>(); // expected-note {{instantiation}}
137 template void check<C<true >, 1, 1, 1, 1, 1, 1>(); // expected-note {{instantiation}}
138 template void check<D<false>, 0, 0, 0, 0, 0, 0>();
139 template void check<D<true >, 1, 1, 1, 1, 1, 1>();
141 // ... the above trick doesn't work for this case...
142 struct Cfalse : virtual A<false> {
143 virtual void f() = 0;
146 Cfalse(const Cfalse&) noexcept;
147 Cfalse(Cfalse&&) noexcept;
149 Cfalse::Cfalse() noexcept = default;
150 Cfalse::Cfalse(const Cfalse&) noexcept = default;
151 Cfalse::Cfalse(Cfalse&&) noexcept = default;