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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:19:05+00:00 2026-05-27T10:19:05+00:00

In C++ is a return value guaranteed to be created before automatic variables in

  • 0

In C++ is a return value guaranteed to be created before automatic variables in the function are destroyed? Notice Basket::get:

class Basket
{
public:
  // Gift is a struct containing safely copyable things like int or string
  Gift gift;
  // Used to protect access and changes to gift
  Mutex mutex;

  // Copy gift into present, while locked to be thread safe
  void put (const Gift & gift)
  {
    Lock lock(mutex);   // Constructor locks, destructor unlocks mutex
    this->gift = gift;  // Gift assignment operator
  }

  // Return a memberwise-copy of gift, tries to be thread safe (but is it?)
  Gift get ()
  {
    Lock lock(mutex);  // Constructor locks, destructor unlocks mutex
    return gift;       // Gift copy constructor
  }
};

I need Basket::get to perform its Gift copy constructor (of the temp object returned) prior to destruction of the lock object. Otherwise the gift object being returned can be corrupted by a simultaneous call to put.

My tests show the gift copy is indeed created before lock destruction, however, is it guaranteed? If not, I’ll need to create a second temporary inside the function, such as:

  Gift get ()
  {
    Gift result;
    {
      Lock lock(mutex);
      result = gift;
    }
    return result;
  }
  • 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-27T10:19:06+00:00Added an answer on May 27, 2026 at 10:19 am

    Yes, the auto variable will remain in scope until after the return is finished. This is especially true if you are using a compiler that optimizes the return, eg:

    Gift get() 
    { 
        Lock lock(mutex);
        return gift;
    } 
    
    Gift g = basket.get();
    

    Which would be equivilent to this sequence:

    Gift g;
    Lock lock(mutex);
    g = Gift(gift);
    lock.~Lock();
    

    May be optimized to act more like this:

    void get(Gift &ret) 
    { 
        Lock lock(mutex);
        ret = gift;
    } 
    
    Gift g;
    basket.get(g);
    

    Which would be equivilent to this sequence:

    Gift g;
    Lock lock(mutex);
    g = gift;
    lock.~Lock();
    

    In other words, a temporary can be removed during the return.

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

Sidebar

Related Questions

Is it possible to get the 'nth' return value from a function without having
Is the return value of GetHashCode() guaranteed to be consistent assuming the same string
What is the return value of the WaitForObject() function? I do not mean the
Possible Duplicate: Return value from thread I want to get the free memory of
Is it possible to inspect the return value of a function in gdb assuming
How can I get the return value of a process? Basically I'm ** ShellExecute
I have the following class: class BritneySpears { public: int getValue() { return m_value;
Is the return value of this function well defined by the C standard ?
Is RVO ( Return Value Optimization ) guaranteed or applicable for all objects and
Consider a third-party class like class A { private int value; public int getValue()

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.