title: Conversion from shared_ptr to weak_ptr polymorphism
body: <p>I am having trouble getting around this one.
  Let's say I have this vector</p>
  
  <pre><code>std::vector&lt;std::shared_ptr&lt;Car&gt;&gt; cars; 
  </code></pre>
  
  <p>Car is an abstract class.
  I want to be able to return weak pointers of different types so I do the following.</p>
  
  <pre><code>template&lt;typename T&gt;
      std::weak_ptr&lt;T&gt; GetCar()
      {
          for (std::vector&lt;std::shared_ptr&lt;Car&gt;&gt;::iterator it = cars.begin(); it != cars.end(); ++it)
          {
              T* derived = dynamic_cast&lt;T*&gt;((*it).get());
              if (derived != nullptr)
              {
                  std::weak_ptr&lt;T&gt; carPointer = *it;
                  return carPointer;
              }
          }
          return std::weak_ptr&lt;T&gt;();
      }
  </code></pre>
  
  <p>I get the following error though when I try to use the function with a class that inherits from Car.
  <code>Error  C2440   'initializing': cannot convert from 'std::shared_ptr&lt;Car&gt;' to 'std::weak_ptr&lt;Saab&gt;'
  </code>
  There might not be a valid car when asked for it. I tried using boost::optional but it does not handle polymorphism . I might go with raw pointers if I can't get this to work.</p>