Bug 55836 - Weffc++: warning: base class 'class std::list<int, std::allocator<int> >' has a non-virtual destructor
Summary: Weffc++: warning: base class 'class std::list<int, std::allocator<int> >' has...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks: Weffc++
  Show dependency treegraph
 
Reported: 2012-12-31 23:22 UTC by Lars Hamrén
Modified: 2018-02-14 13:07 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Self-contained source file (167 bytes, text/x-c++src)
2012-12-31 23:22 UTC, Lars Hamrén
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Lars Hamrén 2012-12-31 23:22:15 UTC
Created attachment 29066 [details]
Self-contained source file

When using -Weffc++ with this code

    class Foo : public std::list<int> { };

the compiler warns that the base class "has a non-virtual destructor". While this is true, there is not much one can do about it.

Suggestions: either remove the warning in these cases (when the problem is in the standard library), or add virtual destructors to standard library classes.

If the compiler were to set a pre-processor variable when -Weffc++ is in effect, adding virtual destructors could be made conditional.

/Lars Hamrén
Comment 1 Andrew Pinski 2013-01-01 00:09:23 UTC
(In reply to comment #0)
> Created attachment 29066 [details]
> Self-contained source file
> 
> When using -Weffc++ with this code
> 
>     class Foo : public std::list<int> { };
> 
> the compiler warns that the base class "has a non-virtual destructor". While
> this is true, there is not much one can do about it.

Why not just not use -Weffc++ for STL.  effc++ warnings are written from a book and they don't always make sense really.
 
> If the compiler were to set a pre-processor variable when -Weffc++ is in
> effect, adding virtual destructors could be made conditional.

Then it would violate the one definition rule.
Comment 2 Lars Hamrén 2013-01-01 00:38:27 UTC
> Why not just not use -Weffc++ for STL.

I can turn it off locally using a pragma, but that is rather ugly.

I like to have as many warning flags turned on as possible. Not having false warnings is a great help.

> effc++ warnings are written from a book
> and they don't always make sense really.

But often they do. Why not give them special treatment when they occur in system headers?

> > If the compiler were to set a pre-processor variable when -Weffc++ is in
> > effect, adding virtual destructors could be made conditional.
> 
> Then it would violate the one definition rule.

Agreed. Bad idea.
Comment 3 Jonathan Wakely 2013-01-01 01:30:50 UTC
(In reply to comment #0)
> When using -Weffc++ with this code
> 
>     class Foo : public std::list<int> { };
> 
> the compiler warns that the base class "has a non-virtual destructor". While
> this is true, there is not much one can do about it.

There are two things you can do:

1) don't use -Weffc++, it's flawed in many ways and noone's forcing you to use it

2) don't use public derivation from classes without virtual destructors, the warning is telling you about a potential problem. If you can't change the base class, take the hint and don't derive from it.
Comment 4 Jason Merrill 2014-05-08 21:21:38 UTC
Adding Nathan to CC, since he's been touching this warning recently.
Comment 5 Jonathan Wakely 2018-02-14 13:07:46 UTC
(In reply to Lars Hamrén from comment #0)
> Suggestions: either remove the warning in these cases (when the problem is
> in the standard library), or add virtual destructors to standard library
> classes.

I think this bug is simply INVALID.

The warning is correct that you're deriving from a class without a virtual destructor, the fact the class is from the standard library doesn't change that. (Bug 56879 suggests limiting the warning to cases that matter, and Bug  	16166 suggests there should be separate options for the various warnings controlled by -Weffc++, which would help here, but still wouldn't change the fact that deriving publicly from std::list is potentially bad).

Giving std::list a virtual destructor is absolutely out of the question.