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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:46:22+00:00 2026-06-17T16:46:22+00:00

Simple example: struct A { A() : i(int()) {} const int& i; }; Error

  • 0

Simple example:

struct A
{
   A() : i(int()) {}
   const int& i;
};

Error from gcc:

a temporary bound to ‘A::i’ only persists until the constructor exits

Rule from 12.2p5:

A temporary bound to a reference member in a constructor’s
ctor-initializer (12.6.2) persists until the constructor exits.

Question

Does anybody know the rationale for this rule? It would seem to me that allowing the temporary to live until reference dies would be better.

  • 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-17T16:46:23+00:00Added an answer on June 17, 2026 at 4:46 pm

    I don’t think the not extending to the object lifetime needs justification. The opposite would!

    The lifetime extension of a temporary extends merely to the enclosing scope, which is both natural and useful. This is because we have tight control on the lifetime of the receiving reference variable. By contrast, the class member isn’t really “in scope” at all. Imagine this:

    int foo();
    struct Bar
    {
        Bar(int const &, int const &, int const &) : /* bind */ { }
        int & a, & b, & c;
    };
    
    Bar * p = new Bar(foo(), foo(), foo());
    

    It’d be nigh impossible to define a meaningful lifetime extension for the foo() temporaries.

    Instead, we have the default behaviour that the lifetime of the foo() temporary extends to the end of the full-expression in which it is contained, and no further.

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

Sidebar

Related Questions

Let's look at a simple example: struct some_struct { std::string str; int a, b,
Very simple example: #include <string> #include <boost/program_options.hpp> namespace po = boost::program_options; int main(int argc,
There is a simple POD data type like struct Item { int value1; double
In this simple example, why do I need to make 'member' const in order
I am trying to run the following PCL simple viewer example from http://pointclouds.org/documentation/tutorials/pcl_visualizer.php I
I'm poking around this unfamiliar territory, and thought I'd try a simple example from
I have a simple code: #include <iostream> #include <functional> struct Copy { Copy(){} Copy(const
I have this very simple example that I am using to learn structs in
For more information see this Example use strict; use warnings; use CGI::Simple; use DBI;
A simple example of what I'd like to achieve: <body> <div style=float:left;> Hey, look

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.