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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:07:56+00:00 2026-05-26T03:07:56+00:00

With the advent of rvalue references on top of Return Value Optimization, what would

  • 0

With the advent of rvalue references on top of Return Value Optimization, what would be the most efficient way to implement a core function like this? How can I improve this implementation or should I leave it alone?

template <typename T>
string
to_string(const T& t)
{ 
  stringstream ss;
  ss << t;  
  return ss.str();
} 

Obviously, I want to avoid copying or allocating memory if I can.
TIA.

Edit: Thx to D. Rodriguez for that detailed answer. Now, I have a second part to my question. Is there a way to improve on this?

#define to_cstr( T ) (to_string( T ).c_str())

Of course I would like to avoid MACROs if I can, but if I copy and paste the template code above to return ss.str().c_str() and const char*, the temporary doesn’t live long enough; although the code seems to run, valgrind complains (red light).

I haven’t been able to come up with a cleaner solution than the MACRO above for to_cstr(). Any ideas how to improve, or should I also leave alone?

  • Ken
  • 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-26T03:07:56+00:00Added an answer on May 26, 2026 at 3:07 am

    Just leave it alone, it is efficient as it is. Even with C++03 compilers, the compiler will optimize the copies away.

    Basically the compiler will make sure that the object in the calling code to to_string, the return statement of to_string, and the return statement of ss.str() all take exactly the same memory location. Which in turn means that there will be no copies.

    Outside of what the standard mandates, the calling conventions for the return statement of a function that returns by value an object that does not fit in the registers in all 32/64 compilers I know (including VS, gcc, intel, suncc) will pass a pointer to the location in memory where the function is to construct the returned object, so that code will be translated internally to something in the lines of:

    // Not valid C++! Just for illustration purposes
    template <typename T>
    to_string( uninitialized<string>* res, const T& t ) {
       stringstream ss;
       ss << t;
       stringstream::str( res, &ss ); // first argument is return location
                                      // second argument is `this`
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to return a noncopyable object of type Foo from a function.
Advert table, Newspaper column takes integer value. Newspaper 1 2 3 Newspaper table like
Since the advent of AS3 I have been working like this: private var loggy:String;
C# has generic function types such as Action<T> or Func<T,U,V,...> With the advent of
I would like to begin work on an iPhone app that does little more
The usual way to use a core data store is to copy it into
For years i've been under the impression that with the advent of anonymous types
With the advent of threading facilities in the STL for the new C++ standard
I have a vision of an advent calendar, where there are pictures of real
I have an advent/christmas calendar. Everyday is another picture with one more door opened.

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.