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

  • SEARCH
  • Home
  • 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 965447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:59:58+00:00 2026-05-16T01:59:58+00:00

In my opinion, the following code (from some C++ question) should lead to UB,

  • 0

In my opinion, the following code (from some C++ question) should lead to UB, but the it seems it is not. Here is the code:

#include <iostream>
using namespace std;
class some{ public: ~some() { cout<<"some's destructor"<<endl; } };
int main() { some s; s.~some(); }

and the answer is:

some's destructor
some's destructor

I learned form c++ faq lite that we should not explicitly call destructor. I think after the explicitly call to the destructor, the object s should be deleted. The program automatically calls the destructor again when it’s finished, it should be UB. However, I tried it on g++, and get the same result as the above answer.

Is it because the class is too simple (no new/delete involved)? Or it’s not UB at all in this case?

  • 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-16T01:59:59+00:00Added an answer on May 16, 2026 at 1:59 am

    The behavior is undefined because the destructor is invoked twice for the same object:

    • Once when you invoke it explicitly
    • Once when the scope ends and the automatic variable is destroyed

    Invoking the destructor on an object whose lifetime has ended results in undefined behavior per C++03 §12.4/6:

    the behavior is undefined if the destructor is invoked for an object whose lifetime has ended

    An object’s lifetime ends when its destructor is called per §3.8/1:

    The lifetime of an object of type T ends when:

    — if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or

    — the storage which the object occupies is reused or released.

    Note that this means if your class has a trivial destructor, the behavior is well-defined because the lifetime of an object of such a type does not end until its storage is released, which for automatic variables does not happen until the end of the function. Of course, I don’t know why you would explicitly invoke the destructor if it is trivial.

    What is a trivial destructor? §12.4/3 says:

    A destructor is trivial if it is an implicitly-declared destructor and if:

    — all of the direct base classes of its class have trivial destructors and

    — for all of the non-static data members of its class that are of class type (or array thereof), each such class has a trivial destructor.

    As others have mentioned, one possible result of undefined behavior is your program appearing to continue running correctly; another possible result is your program crashing. Anything can happen and there are no guarantees whatsoever.

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

Sidebar

Related Questions

I have the following code which does nothing but reading some values from a
Please your opinion on the following code. I need to calculate the diff in
I have the following python code which in my opinion behaves strangely: Imported module:
The following program crashes with a bad glibc double free error: #include <iostream> #include
I've got some legacy code here that makes use of boost::filesystem in version 1.34.1.
I would like to know other people's opinion on the following style of writing
I'm looking for opinion on using Canvas vs. Grid panels in WPF. I need
In your opinion, should the documents listing what further needs to be done on
This is strictly an opinion/experience question for research purposes. I was wondering what coding
I need an opinion and advise from experienced ASP.NET people, what way to go.

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.