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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:37:21+00:00 2026-05-18T02:37:21+00:00

I am looking at code of the following form: class foo { public: foo()

  • 0

I am looking at code of the following form:

class foo
{
  public:
    foo() {}

  //...
};

class bar
{
  public:
    bar() : ref() {}

  private:
    const foo &ref;
};

Is initializing a reference using a temporary in this way correct? I know that it is possible to initialize a const reference that’s a local variable with a temporary, and that doing so extends the lifetime of the temporary, e.g.

const foo &tmp = funcThatReturnsByValue(); //OK

However, one of the answers to the related initialize reference in initialization list suggests that there is a difference between “short-lived” and “long-lived” references, and that initializing ref as above is undefined behavior (even though ref is a const reference).

12.2.5 in the standard says, in part, “A temporary bound to a reference member in a constructor’s ctor-initializer persists until the constructor exits.” Is that describing this situation?

  • 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-18T02:37:22+00:00Added an answer on May 18, 2026 at 2:37 am

    This code is ill-formed. You can’t default initialize or value initialize a reference.

    If you actually had an expression inside of ref(), then yes, 12.2.5 would apply and the temporary would be destroyed when the constructor exits.

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

Sidebar

Related Questions

Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
I'm using the following code to display a context menu on my form when
I'm looking at the following code snippet: my @ret = <someMethod> return (undef) if(
I have the following html.erb code that I'm looking to move to Haml: <span
I'm interested in speed, not good looking code, that is why I'm using array
I am testing some weird-looking CSS code that I wrote (I'm using a mix
I have the following situation in my python code: class Parent(object): def run(self): print
Looking for code samples to access a memory map file in C#
I was looking at code in WPF which is in VB. I am trying
I have a piece of code looking like this : TAxis *axis = 0;

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.