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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:51:55+00:00 2026-05-25T12:51:55+00:00

A point from N3290 C++ draft, § 12.2, 5th point, line 10. The second

  • 0

A point from N3290 C++ draft, § 12.2, 5th point, line 10.

The second context is when a reference is bound to a temporary. The
temporary to which the reference is bound or the temporary that is the
complete object of a subobject to which the reference is bound
persists for the lifetime of the reference except:

A temporary bound to a reference in a new-initializer (5.3.4)
persists until the completion of the full-expression containing the
new-initializer. [ Example:

struct S { int mi; const std::pair<int,int>& mp; };
S a { 1, {2,3} };
S* p = new S{ 1, {2,3} };// Creates dangling reference

— end example ] [ Note: This may introduce a dangling reference,
and implementations are encouraged to issue a warning in such a
case. — end note ]

This is the added point when compared to C++03. But the example is not understandable for me. Can you please explain this point with any other example?

I know what dangling references and temporary objects are and that std::pair holds two values of possibly different data types.

  • 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-25T12:51:56+00:00Added an answer on May 25, 2026 at 12:51 pm

    Temporaies in general last only to the end of the expression that they were created in:

    #include <complex>
    
    
    void func()
    {
        std::complex<int>   a; // Real variable last until the end of scope.
    
        a = std::complex<int>(1,2) + std::complex<int>(3,4);
         // ^^^^^^^^^^^^^^^^^^^^^^  Creates a temporary object
         //                         This is destroyed at the end of the expression.
         // Also note the result of the addition creates a new temporary object
         // Then uses the assignment operator to change the variable 'a'
         // Both the above temporaries and the temporary returned by '+'
         // are destroyed at ';'
    

    If you create a temporary object and bind it to a reference. You extend its lifespan to the same lifespan of the reference it is bound too.

        std::complex<int> const& b  = std::complex<int>(5,6);
                          //           ^^^^^^^^^^^^^^^^ Temporary object
                          // ^^^^                       Bound to a reference.
                          //                            Will live as long as b lives 
                          //                            (until end of scope)
    

    The exception to this rule is when the temporary is bound to a reference in a new initializer.

        S* p1 = new S{ 1, {2,3} };
        // This is the new C++11 syntax that does the `equivalent off`:
    
        S* p2 = new S {1, std::pair<int,int>(2,3) };
                     //   ^^^^^^^^^^^^^^^^^^^^^^^    Temporary object.
                     //                              This lives until the end of the 
                     //                              expression that belongs to the new.
                     //                              ie the temporary will be destroyed
                     //                              when we get to the ';'
    

    But here we are binding the new temporary object to the member

    const std::pair<int,int>& mp;

    This is a const reference. But the temporary object it is bound to will be destroyed at the ‘;’ in the above expression so mp will be a reference to an object that no longer exists when you try and use it in subsequent expressions.

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

Sidebar

Related Questions

A point from n3290 ISO draft: Lambda expressions : section 5.1.2, para 6: The
A point from the ISO C++ Draft n3290 : 3.4.0 2nd point A name
A point from N3290 draft ISO Standard, §3.4.1/12: During the lookup of a name
A point from ISO draft n3290 section 3.4.2 paragraph 1: When the postfix-expression in
A point from ISO draft n3290 section 3.3.9 paragraph 4: The declarative region of
A point from ISO draft N3290 : Unqualified Name Lookup :section 3.4.1, para 14:
According to this link A point from C++0x draft : n3290 Shall this program
A point from ISO C++ DRAFT n3290 :Argument dependant Name Lookup : section 3.4.2,
A point from the ISO C++ draft (n3290): 3.4.2/3 Argument Dependant Name Lookup: Let
A point from n3290 draft §12.1 (Constructors) ¶5: An implicitly-declared default constructor is an

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.