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

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!!

  • 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. 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

Related Questions

Reading C++ Templates: The Complete Guide and it says Note that templates cannot be
I was reading Stroustrup's c++ FAQ and I noticed that, at one point, he
After reading the faq's and everything else I can find, I'm still confused. If
I was reading the C++ FAQ and it says The C++ language guarantees a
The usocket FAQ suggests that the way I should do this is by reading
After reading monkeytalk faq from http://www.gorillalogic.com/testing-tools/monkeytalk/documentation/monkeytalk-faq : How does it all work? MonkeyTalk is
I was reading WCF .NET 4 faq. I'm not sure with the answer for
Reading the MCIMX50 Application Processor Reference Manuals on page 1368 (Section 33.3) there is
I'm reading a book C progaming faq's. Here is passage of the book Automatic
Lately, I've been reading much about constructors from the well-received C++ FAQ . One

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.