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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:47:00+00:00 2026-05-12T20:47:00+00:00

is it possible to return a reference from a function like in this example

  • 0

is it possible to return a reference from a function like in this example code:

string &erase_whitespace(string &text)
{
    text.erase(**etc.**);
    return text;
}

Call:

string text = erase_whitespace(string("this is a test"));
cout << test;

Does this code work? On Visual C++ it does not crash but it looks wrong.

Thanks

  • 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-12T20:47:00+00:00Added an answer on May 12, 2026 at 8:47 pm

    From § 12.2.3 of the C++ 2003 standard (draft)

    Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created.

    § 12.2.4:

    There are two contexts in which temporaries are destroyed at a different point than the end of the full-
    expression. …

    § 12.2.5:

    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 to a subobject of which the temporary is bound persists
    for the lifetime of the reference except as specified below. … A temporary bound to a reference
    parameter in a function call (5.2.2) persists until the completion of the full expression containing the call.

    §8.5.3.5 is what determines when the reference must be a const type. It is possible for a temporary to be bound to a non-const reference if the temporary is an instance of a class that has a conversion operator that returns an appropriate reference (that’s a mouthful). An example might be easier to understand:

    class Foo {
        ...
        operator Bar&() const;
    ...
    void baz(Bar &b);
    ...
        baz(Foo()); // valid
        baz(Bar()); // not valid
    

    The last line isn’t valid because of § 12.3.2.1, which states “A conversion function is never used to convert [an …] object to the […] same object type (or a reference to
    it)”. You might be able to make it work using casting via an ancestor of Bar and a virtual conversion function.

    An assignment is an expression (§ 5.17), thus the full-expression (§ 1.9.12) in your code is the assignment. This gives the following sequence (forgetting for the moment that a temporary string probably can’t be bound to a non-const reference):

    1. A temporary string is created
    2. The temporary is bound to the string& text argument of erase_whitespace
    3. erase_whitespace does its thang.
    4. erase_whitespace returns a reference to the temporary
    5. The temporary is copied to string text
    6. The temporary is destroyed.

    So all is kosher in this case. The problem case, as Mike Seymour points out, would be assigning the result of erase_whitespace to a reference. Note that this likely wouldn’t cause an immediate problem, as the area that stored the string probably contains the same data it did before the temporary was destroyed. The next time something is allocated on the stack or heap, however…

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

Sidebar

Ask A Question

Stats

  • Questions 228k
  • Answers 228k
  • 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 jCarouselLite itself doesn't provide any easy way to stop the… May 13, 2026 at 1:41 am
  • Editorial Team
    Editorial Team added an answer When you use index to perform essentially sequential access to… May 13, 2026 at 1:41 am
  • Editorial Team
    Editorial Team added an answer Use implode: $names = array('Joe', 'Bob', 'Foobar'); echo implode(', ',… May 13, 2026 at 1:41 am

Related Questions

I added a reference to a custom assembly in a report services (2008) report.
I'm looking for insight into the heads of HashSet designers. As far as I
Is there any way to access a dynamically accessing member of a User- defined
Is it at all possible to disable the The webpage you are viewing is

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.