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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:01:37+00:00 2026-06-16T22:01:37+00:00

Consider the following situation: struct X { … }; struct Y { Y(…); …

  • 0

Consider the following situation:

struct X { ... };

struct Y
{
     Y(...);

     ...

     X x;

     ...
}

X f()
{
    ...

    if (...)
        throw E;

    ...
}

Y::Y(...) :
    ... ,
    x(f()) ,
    ...
{
    ...
}

Is there any undefined behaviour here? Are there any gotchas or potential problems with such a design when E is thrown?

Update:

Y::x may not be the only member variable of Y, may not be the first member variable in the class, and may be initialized half way through the Y::Y init list.

  • 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-16T22:01:38+00:00Added an answer on June 16, 2026 at 10:01 pm

    Is there any undefined behaviour here?

    No. The construction of Y is aborted.

    Are there any gotchas or potential problems with such a design when E is thrown?

    No Y is ever created, so there is no destructor to call. Destructors for fully constructed elements, those declared before x in the class body, will be called automatically (how would you otherwise know how far the initialization list you were when it throw). If you need to destroy anything, then you must catch the exception and destroy it yourself. You cannot absorb the exception, you can rethrow it, throw a new exception, or do nothing which will result in the exception being rethrown anyway. For your particular use case, there shouldn’t be anything to destroy since the constructor body is not going to be invoked at all.

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

Sidebar

Related Questions

Consider following situation: there is ComboBox and a filter TextBox, then user types a
Consider the following situation: There is an xml that contains data @XmlData The @XmlData
Consider the following situation: There is a serialization file, created by the older version
Lets consider the following situation. There is products_controller which can be accessed from Admin
Consider the following situation: There is a master page with a contentPlaceHolder for all
Consider the following situation Code was added to the trunk at revision x A
Consider the following situation: WidgetCompany produced a .NET DLL in 2006 called Widget.dll, version
Consider the following situation: We have two Localizable.string files, one in en.lproj and one
Consider the following situation: I have two android projects named P1 and P2 which
Consider the following code: struct A { virtual void f() = 0; }; struct

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.