reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall -Wno-unused-local-typedefs %s

template<bool b> struct ExceptionIf { static int f(); };
template<> struct ExceptionIf<false> { typedef int f; };

// The exception specification of a defaulted default constructor depends on
// the contents of in-class member initializers. However, the in-class member
// initializers can depend on the exception specification of the constructor,
// since the class is considered complete within them. We reject any such cases.
namespace InClassInitializers {
  // Noexcept::Noexcept() is implicitly declared as noexcept(false), because it
  // directly invokes ThrowSomething(). However...
  //
  // If noexcept(Noexcept()) is false, then Noexcept() is a constant expression,
  // so noexcept(Noexcept()) is true. But if noexcept(Noexcept()) is true, then
  // Noexcept::Noexcept is not declared constexpr, therefore noexcept(Noexcept())
  // is false.
  bool ThrowSomething() noexcept(false);
  struct ConstExpr { // expected-error {{default member initializer for 'b' needed}}
    bool b = // expected-note {{declared here}}
      noexcept(ConstExpr()) && ThrowSomething(); // expected-note {{in evaluation of exception spec}}
  };

  // Much more obviously broken: we can't parse the initializer without already
  // knowing whether it produces a noexcept expression.
  struct TemplateArg { // expected-error {{default member initializer for 'n' needed}}
    int n = // expected-note {{declared here}}
      ExceptionIf<noexcept(TemplateArg())>::f(); // expected-note {{in evaluation of exception spec}}
  };

  // And within a nested class.
  struct Nested {
    struct Inner { // expected-error {{default member initializer for 'n' needed}}
      int n = // expected-note {{declared here}}
        ExceptionIf<noexcept(Nested())>::f(); // expected-note {{in evaluation of exception spec}}
    } inner; // expected-note {{in evaluation of exception spec}}
  };

  struct Nested2 {
    struct Inner;
    int n = Inner().n; // expected-note {{in evaluation of exception spec}}
    struct Inner { // expected-error {{initializer for 'n' needed}}
      int n = ExceptionIf<noexcept(Nested2())>::f(); // expected-note {{declared here}}
    } inner;
  };
}

namespace ExceptionSpecification {
  struct Nested {
    struct T {
      T() noexcept(!noexcept(Nested())); // expected-note {{in evaluation of exception spec}}
    } t; // expected-error{{exception specification is not available until end of class definition}}
  };
}

namespace DefaultArgument {
  struct Default {
    struct T {
      T(int = ExceptionIf<noexcept(Default())::f()); // expected-error {{call to implicitly-deleted default constructor}}
    } t; // expected-note {{has no default constructor}}
  };
}

namespace ImplicitDtorExceptionSpec {
  struct A {
    virtual ~A();

    struct Inner {
      ~Inner() throw();
    };
    Inner inner;
  };

  struct B {
    virtual ~B() {} // expected-note {{here}}
  };

  struct C : B {
    virtual ~C() {}
    A a;
  };

  struct D : B {
    ~D(); // expected-error {{more lax than base}}
    struct E {
      ~E();
      struct F {
        ~F() throw(A);
      } f;
    } e;
  };
}

struct nothrow_t {} nothrow;
void *operator new(decltype(sizeof(0)), nothrow_t) noexcept;

namespace PotentiallyConstructed {
  template<bool NE> struct A {
    A() noexcept(NE);
    A(const A&) noexcept(NE);
    A(A&&) noexcept(NE);
    A &operator=(const A&) noexcept(NE);
    A &operator=(A&&) noexcept(NE);
    ~A() noexcept(NE);
  };

  template<bool NE> struct B : virtual A<NE> {};

  template<bool NE> struct C : virtual A<NE> {
    virtual void f() = 0; // expected-note 2{{unimplemented}}
  };

  template<bool NE> struct D final : C<NE> {
    void f();
  };

  template<typename T, bool A, bool B, bool C, bool D, bool E, bool F> void check() {
    T *p = nullptr;
    T &a = *p;
    static_assert(noexcept(a = a) == D, "");
    static_assert(noexcept(a = static_cast<T&&>(a)) == E, "");
    static_assert(noexcept(delete &a) == F, "");

    // These are last because the first failure here causes instantiation to bail out.
    static_assert(noexcept(new (nothrow) T()) == A, ""); // expected-error 2{{abstract}}
    static_assert(noexcept(new (nothrow) T(a)) == B, "");
    static_assert(noexcept(new (nothrow) T(static_cast<T&&>(a))) == C, "");
  }

  template void check<A<false>, 0, 0, 0, 0, 0, 0>();
  template void check<A<true >, 1, 1, 1, 1, 1, 1>();
  template void check<B<false>, 0, 0, 0, 0, 0, 0>();
  template void check<B<true >, 1, 1, 1, 1, 1, 1>();
  template void check<C<false>, 1, 1, 1, 0, 0, 0>(); // expected-note {{instantiation}}
  template void check<C<true >, 1, 1, 1, 1, 1, 1>(); // expected-note {{instantiation}}
  template void check<D<false>, 0, 0, 0, 0, 0, 0>();
  template void check<D<true >, 1, 1, 1, 1, 1, 1>();

  // ... the above trick doesn't work for this case...
  struct Cfalse : virtual A<false> {
    virtual void f() = 0;

    Cfalse() noexcept;
    Cfalse(const Cfalse&) noexcept;
    Cfalse(Cfalse&&) noexcept;
  };
  Cfalse::Cfalse() noexcept = default;
  Cfalse::Cfalse(const Cfalse&) noexcept = default;
  Cfalse::Cfalse(Cfalse&&) noexcept = default;
}