Note that there are some explanatory texts on larger screens.

plurals
  1. POCasting a container of shared_ptr
    primarykey
    data
    text
    <p>I have a method</p> <pre><code>void foo(list&lt;shared_ptr&lt;Base&gt;&gt;&amp; myList); </code></pre> <p>Which I'm trying to call with a two different types of lists, one of DerivedClass1 and one of DerivedClass2</p> <pre><code>list&lt;shared_ptr&lt;DerivedClass1&gt;&gt; myList1; foo(myList1); list&lt;shared_ptr&lt;DerivedClass2&gt;&gt; myList2; foo(myList2); </code></pre> <p>However this obviously generates a compiler error </p> <pre><code>error: a reference of type "std::list&lt;boost::shared_ptr&lt;Base&gt;, std::allocator&lt;boost::shared_ptr&lt;Base&gt;&gt;&gt; &amp;" (not const-qualified) cannot be initialized with a value of type "std::list&lt;boost::shared_ptr&lt;DerivedClass1&gt;, std::allocator&lt;boost::shared_ptr&lt;DerivedClass1&gt;&gt;&gt;" </code></pre> <p>Is there any easy way to cast a container of shared_ptr? Of alternate containers that can accomplish this?</p> <p><strong>Update</strong>: 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). </p> <p>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). </p> <p>Thanks again, you guys are all awesome!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload