title: Can I cast std::vector<Animal*> to std::vector<Dog*> without looking at each element?
body: <p>I have a base class with several classes extending it. I have some generic library utilities that creates a vector containing pointers to the base class so that any of the subclasses will work. How can I cast all of the elements of the vector to a specific child class?</p>
  
  <pre><code>// A method is called that assumes that a vector containing
  // Dogs casted to Animal is passed.
  void myDogCallback(vector&lt;Animal*&gt; &amp;animals) {
      // I want to cast all of the elements of animals to
      // be dogs.
      vector&lt;Dog*&gt; dogs = castAsDogs(animals);
  }
  </code></pre>
  
  <p>My naive solution would look something like this:</p>
  
  <pre><code>// A method is called that assumes that a vector containing
  // Dogs casted to Animal is passed.
  void myDogCallback(vector&lt;Animal*&gt; &amp;animals) {
      // I want to cast all of the elements of animals to
      // be dogs.
      vector&lt;Dog*&gt; dogs;
      vector&lt;Animal*&gt;::iterator iter;
      for ( iter = animals.begin(); iter != animals.end(); ++iter ) {
          dogs.push_back(dynamic_cast&lt;Dog*&gt;(*iter));
      }
  }
  </code></pre>