title: Conversion from STL vector of subclass to vector of base class
body: <p>I am wondering if it is possible to convert a vector of derived class values to a vector of base class values.  Specifically I want to be able to pass a vector of base class objects to a function whose formal parameters takes a vector of base class.  It does not appear to be possible directly as the following code example produces an error (using g++):</p>
  
  <pre><code>#include &lt;vector&gt;
  
  class A {
  };
  
  class B : public A {
  };
  
  
  void function(std::vector&lt;A&gt; objs) {
  }
  
  int main(int argc, char **argv) {
      std::vector&lt;B&gt; objs_b;
      objs_b.push_back(B());
      function(objs_b);
  }
  </code></pre>
  
  <blockquote>
    <p>test.cc:16: error: conversion from ‘std::vector&lt;B, std::allocator&lt;B> >’ to non-scalar type ‘std::vector&lt;A, std::allocator&lt;A> >’ requested</p>
  </blockquote>
  
  <p>I would like to be able to be able to call function without having to define a new vector with elements of type A, inserting my elements of type B or changing to a vector of pointers.</p>