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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:11:15+00:00 2026-06-04T03:11:15+00:00

Let’s say I have a class with a method that returns a shared_ptr .

  • 0

Let’s say I have a class with a method that returns a shared_ptr.

What are the possible benefits and drawbacks of returning it by reference or by value?

Two possible clues:

  • Early object destruction. If I return the shared_ptr by (const) reference, the reference counter is not incremented, so I incur the risk of having the object deleted when it goes out of scope in another context (e.g. another thread). Is this correct? What if the environment is single-threaded, can this situation happen as well?
  • Cost. Pass-by-value is certainly not free. Is it worth avoiding it whenever possible?

Thanks everybody.

  • 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-04T03:11:16+00:00Added an answer on June 4, 2026 at 3:11 am

    Return smart pointers by value.

    As you’ve said, if you return it by reference, you won’t properly increment the reference count, which opens up the risk of deleting something at the improper time. That alone should be enough reason to not return by reference. Interfaces should be robust.

    The cost concern is nowadays moot thanks to return value optimization (RVO), so you won’t incur a increment-increment-decrement sequence or something like that in modern compilers. So the best way to return a shared_ptr is to simply return by value:

    shared_ptr<T> Foo()
    {
        return shared_ptr<T>(/* acquire something */);
    };
    

    This is a dead-obvious RVO opportunity for modern C++ compilers. I know for a fact that Visual C++ compilers implement RVO even when all optimizations are turned off. And with C++11’s move semantics, this concern is even less relevant. (But the only way to be sure is to profile and experiment.)

    If you’re still not convinced, Dave Abrahams has an article that makes an argument for returning by value. I reproduce a snippet here; I highly recommend that you go read the entire article:

    Be honest: how does the following code make you feel?

    std::vector<std::string> get_names();
    ...
    std::vector<std::string> const names = get_names();
    

    Frankly, even though I should know better, it makes me nervous. In principle, when get_names()
    returns, we have to copy a vector of strings. Then, we need to copy it again when we initialize
    names, and we need to destroy the first copy. If there are N strings in the vector, each copy
    could require as many as N+1 memory allocations and a whole slew of cache-unfriendly data accesses > as the string contents are copied.

    Rather than confront that sort of anxiety, I’ve often fallen back on pass-by-reference to avoid
    needless copies:

    get_names(std::vector<std::string>& out_param );
    ...
    std::vector<std::string> names;
    get_names( names );
    

    Unfortunately, this approach is far from ideal.

    • The code grew by 150%
    • We’ve had to drop const-ness because we’re mutating names.
    • As functional programmers like to remind us, mutation makes code more complex to reason about by undermining referential transparency and equational reasoning.
    • We no longer have strict value semantics for names.

    But is it really necessary to mess up our code in this way to gain efficiency? Fortunately, the answer turns out to be no (and especially not if you are using C++0x).

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

Sidebar

Related Questions

Let's say I have a structure named vertex with a method that adds two
Let's say you have a class library project that has any number of supplemental
Let's say I have an abstract parent class called shape, and that there are
let's say I have a class that does some calculations. This set of calculations
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say I have one class User, and it has a property of type
Let's say that I have a set of relations that looks like this: relations
Let's say you have a class called Customer, which contains the following fields: UserName

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.