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

  • SEARCH
  • Home
  • 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 768669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:13:06+00:00 2026-05-14T18:13:06+00:00

I have a method void foo(list<shared_ptr<Base>>& myList); Which I’m trying to call with a

  • 0

I have a method

void foo(list<shared_ptr<Base>>& myList); 

Which I’m trying to call with a two different types of lists, one of DerivedClass1 and one of DerivedClass2

list<shared_ptr<DerivedClass1>> myList1; 
foo(myList1);
list<shared_ptr<DerivedClass2>> myList2; 
foo(myList2);

However this obviously generates a compiler error

error: a reference of type "std::list<boost::shared_ptr<Base>, std::allocator<boost::shared_ptr<Base>>> &" (not const-qualified) cannot be initialized with a value of type "std::list<boost::shared_ptr<DerivedClass1>, std::allocator<boost::shared_ptr<DerivedClass1>>>"

Is there any easy way to cast a container of shared_ptr? Of alternate containers that can accomplish this?

Update: Thanks to everyone who responded. Working within the confines of the language, it seems the best way to go while keeping the method ‘as-is’ is to use a container of shared_ptr and pass exactly that in (creating a new list at the call site).

I guess I pretty much already knew this, but I remembered reading about other parts of the boost library dealing with containers of shared_ptr and thought maybe it was solved more elegantly by someone else already. From my own further research however these seem to be geared more towards reducing overhead of shared_ptr in cases where a number of pointers are owned exclusively (therefore requiring a single lock per container rather than one per object in the container).

Thanks again, you guys are all awesome!

  • 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-14T18:13:06+00:00Added an answer on May 14, 2026 at 6:13 pm

    You can’t cast a container of one type to a container of another type. There are a few ways to create a new container from an existing container, if the type of object stored by the existing container is convertible to the type of object stored by the new container:

    You can use std::copy to do the conversion element-by-element:

    list<shared_ptr<Base> > baseList;
    list<shared_ptr<Derived> > derivedList;
    std::copy(derivedList.begin(), derivedList.end(), std::back_inserter(baseList));
    

    You can also directly construct baseList using the begin and end iterators from derivedList:

    list<shared_ptr<Base> > baseList(derivedList.begin(), derivedList.end());
    

    If your function can take a const reference instead, you can construct a temporary in the function call:

    typedef list<shared_ptr<Base> > BaseList;
    foo(BaseList(derivedList.begin(), derivedList.end()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method which takes params object[] such as: void Foo(params object[] items)
I have an interface method public void Execute(ICommand command); which needs to pass known
I have this method: private delegate void watcherReader(StreamReader sr); private void watchProc(StreamReader sr) {
If I have a method such as: public void MyMethod(int arg1, string arg2) How
If I have a method such as: private function testMethod(param:string):void { // Get the
I have the following C++ method : __declspec(dllexport) void __stdcall getDoubles(int *count, double **values);
I have a C++ method signature that looks like this: static extern void ImageProcessing(
I have an Obj-C method similar to this: -(void)getUserDefaults:(BOOL *)refreshDefaults { PostAppDelegate *appDelegate =
Say you have these two methods: Number 1: void AddPerson(Person person) { // Validate
I have class method that returns a list of employees that I can iterate

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.