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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:17:17+00:00 2026-06-17T18:17:17+00:00

Possible Duplicate: In C++, is it still bad practice to return a vector from

  • 0

Possible Duplicate:
In C++, is it still bad practice to return a vector from a function?

In terms of performance, when needing to return’heavier’ objects like std::vector or std::string from a function, is it recommended to use this form:

void func(std::vector<int> *dest)
{
}

instead of this form:

std::vector<int> func()
{
    std::vector<int> arr;
    // ...
    return arr;
}

I am assuming that the first form should be faster, but at the same time I’ve seen the second form often enough, the Qt API often returns a QString for example, probably because it is much more convenient or intuitive to use.

Also I’ve wondered if there are compiler optimizations which could remove the unnecessary copying of objects when using the return statement.


Edit

Are there any popular compilers still used today which do not perform the optimizations mentioned in the answers?

  • 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-17T18:17:18+00:00Added an answer on June 17, 2026 at 6:17 pm

    is it recommended to use [pass by pointer] instead of [return by value]?

    No.

    A modern C++ compiler performs named return value optimisation (NRVO) which effectively means that the compiler reliably elides the copy here. No copy is performed.

    Note that this is regardless of which C++ version you are using: C++03 does it as well as C++11. The only thing that changed in C++11 is that the language makes it easier for libraries to make moving out of a value (as is happening here) efficient when no copy elision can be performed.

    For return values, copy elision can normally be performed – it’s more relevant in other cases (passing parameters by value, for instance). There are exceptions though; the following code cannot use named return value optimisation. It can use a C++11 move though:

    std::string foo() {
        std::string one = "Foo";
        std::string two = "Bar";
    
        if (rand() % 2 == 0)
            return one;
        else
            return two;
    }
    

    The reason is that now two code paths return different named objects; this prevents NRVO.

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

Sidebar

Related Questions

Possible Duplicate: C++ delete - It deletes my objects but I can still access
Possible Duplicate: How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes? I
Possible Duplicate: C++ delete - It deletes my objects but I can still access
Possible Duplicate: pthread Function from a Class I am getting an error (Can not
Possible Duplicate: Help needed with unloading .DLL’s from AppDomain - Still not working even
Possible Duplicate: Render a variable during creation of anonymous PHP function I am still
Possible Duplicate: C++ delete - It deletes my objects but I can still access
Possible Duplicate: curly braces in string I still don't know what it's called. Like:
Possible Duplicate: In Java, does return trump finally? Wondering if finally statement will still
Possible Duplicate: C++ delete - It deletes my objects but I can still access

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.