Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 537
skypjack Avatar asked Sep 25 '22 04:09

skypjack


1 Answers

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.

like image 188
NickLamp Avatar answered Oct 20 '22 14:10

NickLamp