Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 691081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:30:10+00:00 2026-05-14T02:30:10+00:00

Consider this classic example used to explain what not to do with forward declarations:

  • 0

Consider this classic example used to explain what not to do with forward declarations:

//in Handle.h file
class Body;

class Handle
{
   public:
      Handle();
      ~Handle() {delete impl_;}
   //....
   private:
      Body *impl_;
};

//---------------------------------------
//in Handle.cpp file

#include "Handle.h"

class Body 
{
  //Non-trivial destructor here
    public:
       ~Body () {//Do a lot of things...}
};

Handle::Handle () : impl_(new Body) {}

//---------------------------------------
//in Handle_user.cpp client code:

#include "Handle.h"

//... in some function... 
{
    Handle handleObj;

    //Do smtg with handleObj...

    //handleObj now reaches end-of-life, and BUM: Undefined behaviour
} 

I understand from the standard that this case is headed towards UB since Body’s destructor is non trivial.
What I’m trying to understand is really the root cause of this.

I mean, the problem seems to be “triggered” by the fact that Handle’s dtor is inline, and so the compiler does something like the following “inline expansion” (almost pseudo-code here).

inline Handle::~Handle()
{
     impl_->~Body();
     operator delete (impl_);
}

In all translation units (only Handle_user.cpp in this case) where a Handle instance gets to be destroyed, right?
I just can’t understand this: ok, when generating the above inline expansion the compiler doesn’t have a full definition of the Body class, but why cannot it simply have the linker resolve for the impl_->~Body() thing and so have it call the Body’s destructor function that’s actually defined in its implementation file?

In other words: I understand that at the point of Handle destruction the compiler doesn’t even know if a (non-trivial) destructor exists or not for Body, but why can’t it do as it always does, that is leave a “placeholder” for the linker to fill in, and eventually have a linker “unresolved external” if that function is really not available?

Am I missing something big here (and in that case sorry for the stupid question)?
If that’s not the case, I’m just curious to understand the rationale behind this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:30:10+00:00Added an answer on May 14, 2026 at 2:30 am

    To combine several answers and add my own, without a class definition the calling code doesn’t know:

    • whether the class has a declared destructor, or if the default destructor is to be used, and if so whether the default destructor is trivial,
    • whether the destructor is accessible to the calling code,
    • what base classes exist and have destructors,
    • whether the destructor is virtual. Virtual function calls in effect use a different calling convention from non-virtual ones. The compiler can’t just “emit the code to call ~Body”, and leave the linker to work out the details later,
    • (this just in, thanks GMan) whether delete is overloaded for the class.

    You can’t call any member function on an incomplete type for some or all of those reasons (plus another that doesn’t apply to destructors – you wouldn’t know the parameters or return type). A destructor is no different. So I’m not sure what you mean when you say “why can’t it do as it always does?”.

    As you already know, the solution is to define the destructor of Handle in the TU which has the definition of Body, same place as you define every other member function of Handle which calls functions or uses data members of Body. Then at the point where delete impl_; is compiled, all the information is available to emit the code for that call.

    Note that the standard actually says, 5.3.5/5:

    if the object being deleted has
    incomplete class type at the point of
    deletion and the complete class has a
    non-trivial destructor or a
    deallocation function, the behavior is
    undefined.

    I presume this is so that you can delete an incomplete POD type, same as you could free it in C. g++ gives you a pretty stern warning if you try it, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If I have no interest in being an active contributor… May 16, 2026 at 11:39 am
  • Editorial Team
    Editorial Team added an answer echo -e "Y\nN\nN\n" | trad install May 16, 2026 at 11:39 am
  • Editorial Team
    Editorial Team added an answer Use: SELECT CASE WHEN T.option1 = ? THEN -1 WHEN… May 16, 2026 at 11:39 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This is a complex question, please consider carefully before answering. Consider this situation. Two
Consider this: One mySQL database that has tables and rows and data within it.
consider this string prison break: proof of innocence (2006) {abduction (#1.10)} i just want
Possible Duplicate: Is there a printf converter to print in binary format? Consider this
Consider the problem in which you have a value of N and you need
I am a classic ASP developer. I know, I should learn .NET, but only
I have the classic problem of a thread pushing events to the incoming queue
I have a classic asp application. I want to post a contest form from
We're implementing a new solution in our classic ASP environment that's using COM interop
As background, I gave an answer to this post a little while ago: Return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.