-1

I see warnings like this

destructor called on non-final 'some_class_type' that has virtual functions but non-virtual destructor

on gnu compilers, but on our internal continuous integration tests that also compiles with clang, this warning becomes an error.

Is this supposed to happen, and if so, why does clang characterize this as an error, but gnu doesn't?

Erel
  • 512
  • 1
  • 5
  • 14
roulette01
  • 1,984
  • 2
  • 13
  • 26
  • 6
    Like most warnings, it's not strictly speaking an error, but it does expose you to easy mistakes. Clang is probably compiling with warnings as errors. – François Andrieux Apr 19 '22 at 13:28
  • 2
    It's not an error by C++ standard. Probably your clang compilation has `-Werror` in compilation options. – Yksisarvinen Apr 19 '22 at 13:28
  • Ah yes, after digging deeper into the compilation log, I do see that flag turned on – roulette01 Apr 19 '22 at 13:35
  • 1
    If in doubt whether or not a virtual destructor is needed, check this: https://stackoverflow.com/questions/461203/when-to-use-virtual-destructors – nielsen Apr 19 '22 at 13:46
  • Yes, put it in. It's cheap insurance against nasty surprises, the warning is there for a reason. – Paul Sanders Apr 19 '22 at 13:53

1 Answers1

1

Is this supposed to happen

According to standard: If the program is well-formed, then the program is supposed to be compiled successfully. The compiler is allowed to provide diagnostic messages. Lack of virtual destructor does not make the program ill-formed, even though it's a likely bug in this case.

Compilers typically have the option to treat warnings as errors. Given such option, the described behaviour is supposed to happen, even though that technically doesn't conform to the standard.

why does clang characterize this as an error

Clang can be configured to treat warnings as errors. Given such option is enabled, that would explain this behaviour.

but gnu doesn't?

Although GCC can also be configured to treat warnings as errors, if such option isn't enabled, that would explain this behaviour.


To solve the problem, declare the destructor as virtual.

eerorika
  • 232,697
  • 12
  • 197
  • 326