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 8440115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:11:50+00:00 2026-06-10T08:11:50+00:00

Possible Duplicate: Goto out of a block: do destructors get called? I know that

  • 0

Possible Duplicate:
Goto out of a block: do destructors get called?

I know that goto operator both in C and C++ is useless in almost all situations, but i want to know the answer for this question only by interest, it has no practical meaning.

Does C++ standard guarantees that in such situations destructors of objects must be called properly?

#include <iostream>

class Foo
{
public:
   Foo() { std::cout << "Foo::Foo() \n"; }
   ~Foo() { std::cout << "Foo::~Foo() \n"; }
};

int main()
{
   {
      std::size_t i = 0;
      _1:
      Foo instance;
      if (!++i)
      {
         goto _1;
      }
   }

   {
      Foo instance;
      goto _2;
   }

   _2:
   ;
}

http://liveworkspace.org/code/06031e6699c8fddda94b8594ccab1387

And what about other stange situations with goto and C++ RAII?

It would be really cool if you can post here the quotes from the C++ standard.

  • 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-06-10T08:11:51+00:00Added an answer on June 10, 2026 at 8:11 am

    Yes. Automatic objects are guaranteed to be destructed when they go out of scope. The fact that the scope was exited using goto has no effect on this rule.

    The only situation that I can think of where this doesn’t hold is if the scope is exited using std::longjmp (in which case behaviour is undefined if there are any objects with destructors).

    n3376

    6.6 Jump statments: [stmt.jump]

    Paragraph 2

    On exit from a scope (however accomplished), objects with automatic storage duration (3.7.3) that have been constructed in that scope are destroyed in the reverse order of their construction. [ Note: For temporaries, see 12.2. —end note] Transfer out of a loop, out of a block, or back past an initialized variable with automatic storage duration involves the destruction of objects with automatic storage duration that are in scope at the point transferred from but not at the point transferred to. (See 6.7 for transfers into blocks). [Note: However, the program can be terminated (by calling std::exit() or std::abort() (18.5), for example) without destroying class objects with automatic storage duration. — end note ]

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

Sidebar

Related Questions

Possible Duplicate: GOTO still considered harmful? I have read articles that advise on refraining
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: C/C++: is GOTO faster than WHILE and FOR? I know this has
Possible Duplicate: Recompile decompiled Java (JD / JAD) source that contains goto instructions what's
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: Breaking out of a nested loop I have this code foreach (___)
Possible Duplicate: Modify the URL without reloading the page I know the subject is
Possible Duplicate: goto keyword in java There is no goto in java, right ?
Possible Duplicate: Automatically go to next line in vim Does anybody know why Vim
Possible Duplicate: GOTO still considered harmful? Recently I've been reading The C Programming Language

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.