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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:53:33+00:00 2026-05-28T20:53:33+00:00

Lets take a look to this two functions: std::string get_string() { std::string ret_value; //

  • 0

Lets take a look to this two functions:

std::string get_string()
{
    std::string ret_value;
    // Calculate ret_value ...
    return ret_value;
}

void process_c_string(const char* s)
{
    std::cout << s << endl;
}

And here are two possible calls of process_c_string with argument returned by get_string.

  1. Without binding const reference to the returned object of get_string.

    process_c_string(get_string().c_str());
    
  2. With binding const reference to the returned object of get_string.

    const std::string& tmp_str = get_string();
    process_c_string(tmp_str.c_str());
    

I know that second way is valid, but what about the first one, what does standard say about this case? Will the temporary object returned by get_string be deleted before process_c_str finished because of there is no const reference to it?

Note: The both versions are ok in MSVC.

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

    The lifetime of the temporary extends for the length of the full expression in which it was created. In your case, the temporary will be destroyed but only after the call to process_c_string completes. As long as the function does not store the pointer for later use, you are fine.

    In the second case (binding of reference), the lifetime of that temporary is extended to be the scope of the reference, but I would advise against that pattern in this particular case. You get the same effect by creating a local string initialized with the temporary and the code is simpler. (From a performance point of view, all compilers elide the potential extra copy in the code, so the cost would be the same)

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

Sidebar

Related Questions

Is it possible to generate comments for closing div tags, lets take this ex.
Take a look at this simple HTML: <div id=wrap1> <iframe id=iframe1></iframe> </div> <div id=warp2>
So lets take a look onto a littel bit modified example code: #include <stdio.h>
Take a look at this awesome code from John P Bloch. What this code
I see this quite a bit with online apps. Lets take, for example, the
This would be useful for consistently naming methods, for example let's take a look
Lets say I have a table with just two columns: name and mood .
I know how to gain access to management-objects. Lets say this one: var searcher
If you take a look at the clock_gettime() function, which is available in all
Take a look at the first example of http://code.google.com/intl/sv-SE/appengine/docs/java/datastore/jdo/relationships.html (Owned One-to-One Relationships). I dont

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.