Casting a container of shared_ptr

Posted by Jamie Cook on Stack Overflow See other posts from Stack Overflow or by Jamie Cook
Published on 2010-05-06T15:05:26Z Indexed on 2010/05/06 15:08 UTC
Read the original article Hit count: 236

Filed under:
|
|
|
|

Hi all,

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost