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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:41:44+00:00 2026-06-06T01:41:44+00:00

Prior to C++11, if I had a function that operated on large objects, my

  • 0

Prior to C++11, if I had a function that operated on large objects, my instinct would be to write functions with this kind of prototype.

void f(A &return_value, A const &parameter_value);

(Here, return_value is just a blank object which will receive the output of the function. A is just some class which is large and expensive to copy.)

In C++11, taking advantage of move semantics, the default recommendation (as I understand it) is the more straightforward:

A f(A const &parameter_value);

Is there ever still a need to do it the old way, passing in an object to hold the return value?

  • 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-06-06T01:41:46+00:00Added an answer on June 6, 2026 at 1:41 am

    Others have covered the case where A might not have a cheap move constructor. I’m assuming your A does. But there is still one more situation where you might want to pass in an “out” parameter:

    If A is some type like vector or string and it is known that the “out” parameter already has resources (such as memory) that can be reused within f, then it makes sense to reuse that resource if you can. For example consider:

    void get_info(std::string&);
    bool process_info(const std::string&);
    
    void
    foo()
    {
        std::string info;
        for (bool not_done = true; not_done;)
        {
            info.clear();
            get_info(info);
            not_done = process_info(info);
        }
    }
    

    vs:

    std::string get_info();
    bool process_info(const std::string&);
    
    void
    foo()
    {
        for (bool not_done = true; not_done;)
        {
            std::string info = get_info();
            not_done = process_info(info);
        }
    }
    

    In the first case, capacity will build up in the string as the loop executes, and that capacity is then potentially reused on each iteration of the loop. In the second case a new string is allocated on every iteration (neglecting the small string optimization buffer).

    Now this isn’t to say that you should never return std::string by value. Just that you should be aware of this issue and apply engineering judgment on a case by case basis.

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

Sidebar

Related Questions

We had the following code prior to Delphi 2009: function MemoryStreamToString(M : TMemoryStream): String;
Hi all I know that this has been posted as a prior question several
I had an idea that I could write a query to find all the
I'm developing an app that was just released. Prior to release, I had 10
Prior to the asp.net 4 RC update, I had been using the WebApi for
Introduction : I have prior experience in programming (C, C++, Java), however, this is
I'm a very new python user (had only a little prior experience with html/javascript
Up to this point my company has had an aversion to databases and has
During a recient PCI audit the auditor said that we had major security risks
I have a .plist that I have had translated in my app. Originally, I

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.