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 164603

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:46:24+00:00 2026-05-11T11:46:24+00:00

I was reading C++ Faq Second Edition , faq number 32.08 . FAQ says

  • 0

I was reading C++ Faq Second Edition , faq number 32.08 .

FAQ says that parameter passed by const reference and returned by const reference can cause dangling reference.

But it is ok if parameter is passed by reference and returned by reference.

I got it that it is unsafe in case of const reference but how is it safe in case when parameter is non const reference.

Last line of FAQ says ‘Note that if a function accepts a parameter by non-const reference (for example, f(string& s)), returning a copy of this reference parameter is safe because a temporary cannot be passed by non-const reference.’

Need some insight on this!!

  • 0 0 Answers
  • 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. 2026-05-11T11:46:25+00:00Added an answer on May 11, 2026 at 11:46 am

    if you have like

    const Foo & bar(const Foo &f) { return f; } 

    and call it like

    const Foo &ret = bar(Foo()); 

    This compiles, but the problem is that now ‘ret’ is a dangling reference, because the temporary object created by the call to Foo() gets freed after bar returns. The detailed execution sequence here is:

    1. temporary Foo is allocated
    2. bar is called with a reference to the temporary object
    3. bar returns the reference
    4. now that bar has returned the temporary Foo is released
    5. the reference is now dangling as the object was destroyed

    However, if you had Foo declared as

    Foo & bar(Foo &f) { return f; } 

    then your call bar(Foo()) would not be accepted by compiler. When you pass a temporary object to a function, you can only take it by const reference or as a copy; this is part of the language definition.

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

Sidebar

Ask A Question

Stats

  • Questions 174k
  • Answers 174k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer MSBuild targets aren't designed to receive parameters. Instead, they use… May 12, 2026 at 2:48 pm
  • Editorial Team
    Editorial Team added an answer From my experience with Wordpress, Joomla and Drupal: It seems… May 12, 2026 at 2:48 pm
  • Editorial Team
    Editorial Team added an answer Tools-->Options... Text Editor-->All Languages Check "Navigation Bar." (Picture is from… May 12, 2026 at 2:48 pm

Related Questions

I was reading the C++0x FAQ by Stroustrup and got stuck with this code.
I have been reading through the C++ FAQ and was curious about the friend
I'm trying to write some STL data out of matlab and I'm trying to
I was reading this page : C++ Tip: How To Get Array Length .

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.