Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destructor in derived class marked as noexcept(false)

Tags:

c++

c++11

There is following code:

class Member
{
public:
  ~Member() noexcept(false) {}
};

class A
{
public:
  virtual ~A() {}
};

class B : public A
{
public:
  Member m;
};

The error is:

main.cpp:13:7: error: looser throw specifier for ‘virtual B::~B() noexcept (false)’
 class B : public A
       ^
main.cpp:10:11: error:   overriding ‘virtual A::~A() noexcept’
   virtual ~A() {}
           ^

Why does the destructor in class B is marked as noexcept(false)? It seems that it somehow gets it from Member class. It was compiled by g++ 6.3.

like image 627
wulujey Avatar asked Aug 30 '17 14:08

wulujey


People also ask

Is destructor noexcept?

Any user-defined destructor is noexcept(true) by default, unless the declaration specifies otherwise, or the destructor of any base or member is noexcept(false) . Any deallocation function is noexcept(true) by default, unless the declaration specifies otherwise.

Should I mark destructor Noexcept?

Item 14, page 94: 'By default, all memory deallocation functions and all destructors -both user-defined and compiler-generated- are implicitly noexcept. There is thus no need to declare them noexcept. (Doing so doesn't hurt anything, it's just unconventional.)'

Does a derived class need a destructor?

No. You never need to explicitly call a destructor (except with placement new). A derived class's destructor (whether or not you explicitly define one) automagically invokes the destructors for base class subobjects. Base classes are destructed after member objects.


1 Answers

B's destructor will destroy m, which is not a noexcept operations. You can't be sure that ~B won't throw, so it is also noexcept(false).

See http://en.cppreference.com/w/cpp/language/destructor#Implicitly-declared_destructor :

[...] In practice, implicit destructors are noexcept unless the class is "poisoned" by a base or member whose destructor is noexcept(false).

like image 74
François Andrieux Avatar answered Oct 01 '22 19:10

François Andrieux