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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:58:40+00:00 2026-05-15T15:58:40+00:00

Say I have a std::list of class T ‘s: std::list<T> l; When passing it

  • 0

Say I have a std::list of class T‘s:

std::list<T> l;

When passing it into functions, I would use a reference:

someFunction( std::list<T> &l )

What is the best way to pass around (the elements) of a std::list of unique_ptrs?

std::list< std::unique_ptr<T> >  l;

Like this:

someFunction( std::unique_ptr<T> ptr )

Or this:

someFunction( T* ptr )

Or this:

someFunction( T &ref )

And what how would I call it using the std::list‘s back() function for example? These are IMHO all “kind of” equivalent, but I’m sure I’m missing something here.

Thanks

  • 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-15T15:58:40+00:00Added an answer on May 15, 2026 at 3:58 pm

    In order of best to worse:

    1. someFunction(const T&);
    2. someFunction(T&);
    3. someFunction(const std::unique_ptr<T>&);
    4. someFunction(std::unique_ptr<T>&);

    The first one is the best because it does not modify the object it and it will work with the object no matter how you have allocated it (for example, you could switch to shared_ptr with no problems).

    Number two will also work regardless of what smart pointer you are using; however, it assumes that you can modify the object, and whenever you can make something const, you should.

    Numbers 3 and 4 both allow the object being pointed-to to be mutated; however, #3 does not allow the smart pointer to be modified, while number 4 does. Both have the disadvantage that they force the use of unique_ptr, whereas the two above it would work regardless of smart pointer class.

    Passing a unique_ptr by value, as you have in some of the other examples is not an option; a unique_ptr is supposed to be unique. If you are copying it, consider using shared_ptr.

    For the first two, if you invoked it on the result of back(), it would look like:

    someFunction(*(lst.back()));  // dereference lst.back() before passing it in.
    

    For the latter two, if you invoked it on the resut of back(), it would look like:

    someFunction(lst.back()); // pass the smart pointer, not the object to
                              // which the smart pointer currently points.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you have a std::list of some class. There are two ways you can
Let's say I have a: std::vector<std::list<Node> > And let's say Node is: class Node
Let's say I have: class myClass std::list<myClass> myList where myClass does not define the
Say I have this: Object *myObject; std::vector<Object> objects(99); .... myObject = &objects[4]; Would it
Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
Let's say you have a c++0x std::array member of a template class and you
Say I have two containers storing pointers to the same objects: std::list<Foo*> fooList; std::vector<Foo*>
I have a class of interest (call it X). I have a std::list<X*> (call
Lets say I have these lines of code; std::vector<int> ints; std::for_each(ints.begin(), ints.end(), [](int& val){

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.