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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:21:07+00:00 2026-06-08T15:21:07+00:00

The function needs to return a shared_ptr pointing to StructA . struct StructA {

  • 0

The function needs to return a shared_ptr pointing to StructA.

struct StructA
{
    // complicated struct that also holds other sub-structure
    ....
};

const boost::shared_ptr<const StructA&>& GetStructA(...)
{...} #0.5

const boost::shared_ptr<const StructA>& GetStructA(...)
{...} #0

const boost::shared_ptr<StructA>& GetStructA(...)
{...} #1

const boost::shared_ptr<StructA> GetStructA(...)
{...} #2

boost::shared_ptr<const StructA> 
{...} #3

boost::shared_ptr<StructA> GetStructA(...)
{...} #4

boost::shared_ptr<StructA>& GetStructA(...)
{...} #5

boost::shared_ptr<StructA&> GetStructA(...)
{...} #6

There are so many choices and I am sure one of them is best(pls point out if there is anyone left).

Personally, I prefer to using #0

const boost::shared_ptr<const StructA&>& GetStructA(...)
{...} #0

The legacy system uses #2

const boost::shared_ptr<StructA>  GetStructA(...)
{...} #2

The reason why I prefer to choosing #0 is as follows:

  1. return const shared_ptr, so that the caller of this function should NOT change the returned shared_ptr which may point to internal data structure

  2. return by reference, so that I can avoid +/- of the reference count of shared_ptr

  3. the shared_ptr holds const StructA&, so that the caller cannot change the content of the const shared_ptr. If I were right, even if the shared_ptr is const, it cannot prevent the caller from changing the pointed data unless the data is const.

Please

  1. correct my understanding if I made any mistake
  2. provide a best return signature for this function.

Thank you

  • 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-08T15:21:12+00:00Added an answer on June 8, 2026 at 3:21 pm

    It will depend on what the function does:

    • Is it creating a new object of StructA? If yes, then you must return a copy of shared_ptr.
    • Is it just providing access to a StructA object that you know will not expire under the returned referece? Then you could return const& on it (but don’t – see below)

    As you yourself suspect, const& on shared_ptr won’t prevent non-const access to the object it points to – it just means that shared_ptr object itself is constant and cannot be reset or pointed to some other object. The semantics of shared_ptr, in this case, are the same as with semantics of the plain pointers.

    I used to use const& idiom a lot when returning access pointers. But at the end it can lead to very subtle bugs especially in multithreaded code (I’m careful but I still got bitten). So peachykeen’s comment above is spot on and I follow that idiom for all new code. Not only for returns mind you but also when a function argument is a shared_ptr. At the end you really want to know that whenever you have an object pointed by a shared_ptr – you really have it and not just a reference of a shared_ptr of a long dead object.

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

Sidebar

Related Questions

Visual C++ is saying my void function needs a return value I compiled this
I have a function that might fail, so the value it returns needs to
For reasons beyond my control, I need to return const char* from a function,
I need a function that can return the difference between the below two dates
I need a function to return a string that will only be accessed read-only.
I need a function that will take a bitmap and return the bitmap with
I need a C# function that takes 2 strings as an input and return
I need to create a function that returns an allocated shared_ptr variable. is this
I need to return multiple values from a ColdFusion function in an ajax callback
I need to design a function to return negative numbers unchanged but should add

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.