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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:22:09+00:00 2026-06-01T04:22:09+00:00

Given the code from here : class lazy_init { mutable std::once_flag flag; mutable std::unique_ptr<expensive_data>

  • 0

Given the code from here:

class lazy_init
{
    mutable std::once_flag flag;
    mutable std::unique_ptr<expensive_data> data;

    void do_init() const
    {
        data.reset(new expensive_data);
    }
public:
    expensive_data const& get_data() const
    {
        std::call_once(flag,&lazy_init::do_init,this);
        return *data;
    }
};

And I saw a few variants of the same pattern elsewhere also. So my question is: why this code is considered save? and why compiler can’t just read data before calling std::call_once and ends up with an incorrect data?
e.g

tmp = data.get();
std::call_once(flag,&lazy_init::do_init,this);
return *tmp;

I mean I have found nothing about any barriers which would prevent that.

  • 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-01T04:22:11+00:00Added an answer on June 1, 2026 at 4:22 am

    Programming in C++ would be essentially impossible if the compiler was allowed to produce code that matched what you describe.

    This is stated in §1.9/14 Program Execution (n3290):

    Every value computation and side effect associated with a full-expression is sequenced before every value
    computation and side effect associated with the next full-expression to be evaluated.

    Your return statement is sequenced after the preceding full expression. The compiler has to output code as if all side-effects of that preceding statement has been completely evaluated before it evaluates the return statement.
    Your example doesn’t respect that rule, since it evaluates *data before taking into account the side-effects of the std::call_once(...) full expression.

    Additionally, std::call_once has this in its description (§30.4.4.2/2 and 3):

    2/ Effects: An execution of call_once that does not call its func is a passive execution. An execution of call_once that calls its func is an active execution. An active execution shall call INVOKE (DECAY_-
    COPY ( std::forward<Callable>(func)), DECAY_COPY (std::forward<Args>(args))...)
    . If such
    a call to func throws an exception the execution is exceptional, otherwise it is returning. An exceptional execution shall propagate the exception to the caller of call_once. Among all executions of call_once for any given once_flag: at most one shall be a returning execution; if there is a returning execution, it shall be the last active execution; and there are passive executions only if there is a returning execution. [ Note: passive executions allow other threads to reliably observe the results produced by the earlier returning execution. — end note ]

    3/ Synchronization: For any given once_flag: all active executions occur in a total order; completion of an active execution synchronizes with the start of the next one in this total order; and the returning execution synchronizes with the return from all passive executions.

    So the standard mandates synchronization to fit your use-case.

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

Sidebar

Related Questions

Given: code inside a stored proc: select bleh into #tblTemp from FunctionThatReturnsTable('some','params') -- do
Given the following code (copied from the attoparsec library) what does the inline pragma
Having been given the all clear to move code from my Page_Loaded method to
So here is my code in View. public class TheView extends View { ..
Ok. So here's a netbeans issue thanks to auto generated code from GUI designer.
UPDATE: found similar question: Rx - unsubscribing from events Given the code: interface IBitmapCapturer
Assume that the following Perl code is given: my $user_supplied_string = &retrieved_from_untrusted_user(); $user_supplied_string =~
On my machine Linux machine ulimit -n gives 1024 . This code: from tempfile
Can any one give me a sample code that gets the selected value from
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });

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.