-1

The scenario is like , I want to inherit my class from some third party library class whose destructor is noexcept(true) by default.and derived class's destructor is noexcept(false) as it can throw exception. There is following code:

class 3PP
{
public:
  virtual ~3PP() {}
};

class B : public 3PP
{
public:
  ~B() noexcept(false)
};

I am getting this error while compilation.

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

Is there any way to inherit it from 3PP class without making its destructor noexcept(false)

I also want to understand limitation of making destructor as noexcept(false).

Ch3steR
  • 20,090
  • 4
  • 28
  • 58

1 Answers1

0

can class having destructor with noexcept(false) be inherited from the base class having destructor with noexcept(true)

Yes. Following program is well-formed:

struct B {
    ~B() noexcept        = default;
};

struct D : B {
    ~D() noexcept(false) = default;
};

However, override of a virtual function may not be potentially throwing if the base function is noexcept. Thus you cannot have a potentially throwing destructor if base has a virtual noexcept destructor.

eerorika
  • 232,697
  • 12
  • 197
  • 326