title: candidate function not viable: no known conversion from std::vector<derived> to std::vector<base>
body: <p>First, The title probably may not reflect the current question, so please feel free to change. Assuming I have the following classes;</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  template &lt;typename K, class V&gt;
  class A {
  public:
    K x;
    V y;
    A(K x, V y):x(x), y(y) {}
    void print(A&lt;K, V&gt;&amp; z) {
      std::cout &lt;&lt; x + z.x &lt;&lt; "-" &lt;&lt; y + z.y &lt;&lt; std::endl;
    }
    void print(std::vector&lt;A&lt;K,V&gt;&gt; z) {
      for(auto&amp; i:z) {
        print(i);
      }
    }
  };
  
  class B:public A&lt;int, std::string&gt; {
  public:
    B():A(0, "zero") {}
    B(int x, std::string y):A(x, y) {}
  };
  
  void test() {
    B b1(1, "one");
    B b2(2, "two");
    B b3(3, "three");
    B b4(4, "four");
    B b5(5, "five");
    b5.print(b1);
    //
    std::vector&lt;B&gt; c;
    c.push_back(b1);
    c.push_back(b2);
    c.push_back(b3);
    c.push_back(b4);
    b5.print(c);
  }
  </code></pre>
  
  <p>I get the following error at last last line (<code>b5.print(c)</code>);</p>
  
  <pre><code>test_class.cpp:40:6: error: no matching member function for call to 'print'
    b5.print(c);
    ~~~^~~~~
  test_class.cpp:10:8: note: candidate function not viable: no known conversion from 'std::vector&lt;B&gt;' to 'A&lt;int, std::__1::basic_string&lt;char&gt; &gt; &amp;' for 1st argument
    void print(A&lt;K, V&gt;&amp; z) {
         ^
  test_class.cpp:13:8: note: candidate function not viable: no known conversion from 'vector&lt;B&gt;' to 'vector&lt;A&lt;int, std::__1::basic_string&lt;char&gt; &gt;&gt;' for 1st argument
    void print(std::vector&lt;A&lt;K,V&gt;&gt; z) {
         ^
  1 error generated.
  </code></pre>
  
  <p>I basically expect an implicit conversion from <code>vector&lt;B&gt;</code> to <code>std::vector&lt;A&lt;int,std::string&gt;&gt;</code> but it is not. Hence, I came up with two solutions to the issue.</p>
  
  <ol>
  <li>Define <code>typedef std::vector&lt;A&lt;int,std::string&gt;&gt; MyWeirdVector;</code>
  in class A and use se <code>B::MyWeirdVector c;</code> instead of
  <code>std::vector&lt;B&gt; c;</code>.</li>
  <li>Define each print function as <code>template &lt;typename U&gt;</code> in A class and accept typename U as argument.</li>
  </ol>
  
  <p>Both solutions has its own drawback. In first, I have to instantiate c as B::MyWeirdVector and in second, I don't (feel like) have a type safety. Second solutions works even if I don't define type in <code>&lt;&gt;</code>.</p>
  
  <p>So, is there an elegant solution to this issue like to let implicit type conversion from <code>std::vector&lt;B&gt;</code> to <code>std::vector&lt;A&lt;int,std::string&gt;&gt;</code>?</p>
  
  <p><strong>-- EDIT --</strong></p>
  
  <p>Thanks to @max66 and @Caleth and other fellows. I just want to share full working example. Please, note that there is no <code>void</code> before <code>print</code> for @max66's answer if you don't want to go crazy. (1. All print function arguments are <code>const</code>, 2. Merge answers from @max66 and @Caleth.)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;type_traits&gt;
  
  template &lt;typename K, class V&gt;
  class A {
  public:
    K x;
    V y;
    A(K x, V y):x(x), y(y) {}
    void print(const A&lt;K, V&gt;&amp; z) {
      std::cout &lt;&lt; x + z.x &lt;&lt; "-" &lt;&lt; y + z.y &lt;&lt; std::endl;
    }
  
    // for C++11, thanks to @Caleth
    // template &lt;typename Container, typename = typename std::enable_if&lt;!std::is_base_of&lt; A&lt;K,V&gt;, typename std::remove_reference&lt;Container&gt;::type &gt;::value&gt;::type&gt;
    // void print(Container&amp;&amp; z) {
    //   for(auto&amp; i:z) {
    //     print(i);
    //   }
    // }
  
    // thanks to @max66
    template &lt;typename T&gt;
    typename std::enable_if&lt;std::is_base_of&lt;A&lt;K, V&gt;, T&gt;::value&gt;::type
      print(std::vector&lt;T&gt; const &amp; z) {
        for(auto const &amp; i:z) print(i);
      }    
    };
  
  class B:public A&lt;int, std::string&gt; {
  public:
    B():A(0, "zero") {}
    B(int x, std::string y):A(x, y) {}
  };
  
  void test() {
    B b1(1, "one");
    B b2(2, "two");
    B b3(3, "three");
    B b4(4, "four");
    B b5(5, "five");
    b5.print(b1);
    //
    std::vector&lt;B&gt; c;
    c.push_back(b1);
    c.push_back(b2);
    c.push_back(b3);
    c.push_back(b4);
    b5.print(c);
  }
  </code></pre>