c++c++11constructornoexceptinheriting-constructors

Are inheriting constructors noexcept(true) by default?


Here I found that:

Inheriting constructors [...] are all noexcept(true) by default, unless they are required to call a function that is noexcept(false), in which case these functions are noexcept(false).

Does it mean that in the following example the inherited constructor is noexcept(true), even though it has been explicitly defined as noexcept(false) in the base class, or it is considered for itself as a function that is noexcept(false) to be called?

struct Base {
    Base() noexcept(false) { }
};

struct Derived: public Base {
    using Base::Base;
};

int main() {
    Derived d;
}

Solution

  • The inherited constructor will also be noexcept(false) because as you quoted an inherited constructor will be noexcept(true) by default

    unless they are required to call a function that is noexcept(false)

    When the Derived constructor runs it will also call the Base constructor which is noexcept(false), therefore, the Derived constructor will also be noexcept(false).

    This is evidenced by the following.

    #include <iostream>
    
    struct Base {
      Base() noexcept(false) { }
    };
    
    struct Derived: public Base {
      using Base::Base;
    };
    
    int main() {
      std::cout << noexcept(Derived());
    }
    

    Outputs 0.