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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:27:09+00:00 2026-05-26T14:27:09+00:00

in a function, which return would be more appropriate? A. vector<Foo> ? B. shared_ptr<vector<Foor>>

  • 0

in a function, which “return” would be more appropriate?

A. vector<Foo> ?

B. shared_ptr<vector<Foor>> ?

In other words, which copy is less heavy, what would you do, and why?

  • 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-26T14:27:10+00:00Added an answer on May 26, 2026 at 2:27 pm

    I think returning shared_ptr<vector<T>> rarely is useful. I would only do this if several objects where to hold a shared vector that they could manipulate. To me this indicates a design flaw. A better alternative is probably to return by const reference. This avoids (a potentially expensive) copy operation, but does not allow the accessor to alter the vector.

    If you are returning a local std::vector you could also return it by argument.

    If you really want to return shared_ptr<vector<T>>, consider if shared_ptr<const vector<T>> would do the job (the vector can be inspected by many, but only manipulated by the owner).

    However A is generally more expensive than B, but return value optimizations often applies here. For C++11 std::vector has a move constructor that will guarantee that returning a local std::vector won’t require expensive copy operations.

    Remember, don’t prematurely optimize 🙂

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

Sidebar

Related Questions

How can I build a function slice(x, n) which would return a list of
I wanted to create a function which would return the path of the current
How can I write a function which will return pi (π) to a given
The goal: Any language. The smallest function which will return whether a string is
I want a function/code which will return the value that the user submitted for
I have a function which returns 3 numbers, e.g.: def numbers(): return 1,2,3 usually
can u please help me how to return an instance of a function which
I have a function to create a pdf which should return bitarray. Below is
I'm trying to return a string from a function. Which basically adds some chars
I have a function which I would like to take in an NSString and

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.