Bug 25826 - "pure virtual" destructors accepted by GCC, but cause link failure
Summary: "pure virtual" destructors accepted by GCC, but cause link failure
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.0.2
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-01-17 19:22 UTC by Jack Lloyd
Modified: 2006-01-17 21:39 UTC (History)
1 user (show)

See Also:
Host: i386-redhat-linux
Target: i386-redhat-linux
Build: i386-redhat-linux
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jack Lloyd 2006-01-17 19:22:09 UTC
The following code:

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

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

int main()
   {
   B b;
   }

compiles with GCC 4.0.2 (clean with -ansi -Wall -Wextra) but does not link due to an undefined reference to ~A(). Herb Sutter claims this code is valid, for whatever that might be worth (http://www.gotw.ca/gotw/031.htm), but in either case this seems to be a bug; either it should be rejected with a diagnostic as invalid code, or it should link (and ideally do something sensible).
Comment 1 Andrew Pinski 2006-01-17 19:27:46 UTC
You still need to declare A::~A().

That is what the following passage from that doc means:

Of course, any derived class' destructor must call the base class' destructor, and so the destructor must still be defined (even if it's empty):

    // file b.cpp
    B::~B() { /* possibly empty */ }
If this definition were not supplied, you could still derive classes from B but they could never be instantiated, which isn't particularly useful.
Comment 2 Jack Lloyd 2006-01-17 19:32:24 UTC
Ah, I misread it, but the bug should stay open IMO - the invalidity of the code reduces it to "GCC doesn't reject invalid code", which is obviously a low priority, but still a bug, no?
Comment 3 Andrew Pinski 2006-01-17 19:33:56 UTC
(In reply to comment #2)
> Ah, I misread it, but the bug should stay open IMO - the invalidity of the code
> reduces it to "GCC doesn't reject invalid code", which is obviously a low
> priority, but still a bug, no?

No, this is not invalid code.  Just useless code.
Comment 4 gdr@cs.tamu.edu 2006-01-17 21:11:37 UTC
Subject: Re:   New: "pure virtual" destructors accepted by GCC, but cause link failure

"lloyd at randombit dot net" <gcc-bugzilla@gcc.gnu.org> writes:

| The following code:
| 
| class A
|    {
|    public:
|       virtual ~A() = 0;

You still need to *define* the destructor.  See ยง12.4/7.

-- Gaby
Comment 5 gdr@cs.tamu.edu 2006-01-17 21:12:37 UTC
Subject: Re:  "pure virtual" destructors accepted by GCC, but cause link failure

"lloyd at randombit dot net" <gcc-bugzilla@gcc.gnu.org> writes:

| Ah, I misread it, but the bug should stay open IMO - the invalidity
| of the code reduces it to "GCC doesn't reject invalid code", which
| is obviously a low priority, but still a bug, no?

the code is rejected -- at link time.  So it is no bug.  
PR should be closed as invalid.

-- Gaby
Comment 6 Jack Lloyd 2006-01-17 21:39:57 UTC
Thank you for the reference Gaby. I'm now not quite sure what purpose a pure virtual destructor has, or why it should be legal, but neither the apparent language oddity nor my confusion about same is a GCC problem, so... Andrew, Gaby, sorry to distract you with the invalid bug report. I'll check the standard first next time.
Comment 7 gdr@cs.tamu.edu 2006-01-17 22:00:17 UTC
Subject: Re:  "pure virtual" destructors accepted by GCC, but cause link failure

"lloyd at randombit dot net" <gcc-bugzilla@gcc.gnu.org> writes:

| I'm now not quite sure what purpose a pure virtual destructor has,

the usefulness used to be subject of debate when OO was "the" main
topic.  Some people believe it is a concise way for them to state that
a given base class for a hierarchy is abstract.

-- Gaby