title: copy_if with different types
body: <p>Is there a modern way of expressing the intent to conditionally copy from a source container of a different type to a destination container if I know how to extract the matching type?</p>
  
  <p>It is easier to pose the question as a code-example:</p>
  
  <pre><code>#include &lt;algorithm&gt;
  #include &lt;vector&gt;
  
  struct Foo {};
  struct FooBar{
      bool is_valid;
      Foo foo;
  };
  
  std::vector&lt;Foo&gt; get_valid_foos(const std::vector&lt;FooBar&gt;&amp; foobars){
      std::vector&lt;Foo&gt; valid_foos;
      for(const auto&amp; fbar : foobars){
          if(fbar.is_valid)
              valid_foos.push_back(fbar.foo);
      }
      return valid_foos;
  }
  
  std::vector&lt;Foo&gt; get_valid_foos_modern(const std::vector&lt;FooBar&gt;&amp; foobars){
      std::vector&lt;Foo&gt; valid_foos;
      std::copy_if(foobars.begin(), foobars.end(), std::back_inserter(valid_foos),
          [](const auto&amp; foobar){
              return foobar.is_valid;
          });
      //?? std::copy requires input and output types to match
      return valid_foos;
  }
  </code></pre>
  
  <p><a href="https://godbolt.org/g/miPbfW" rel="noreferrer">https://godbolt.org/g/miPbfW</a></p>