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?
It is easier to pose the question as a code-example:
#include <algorithm>
#include <vector>
struct Foo {};
struct FooBar{
bool is_valid;
Foo foo;
};
std::vector<Foo> get_valid_foos(const std::vector<FooBar>& foobars){
std::vector<Foo> valid_foos;
for(const auto& fbar : foobars){
if(fbar.is_valid)
valid_foos.push_back(fbar.foo);
}
return valid_foos;
}
std::vector<Foo> get_valid_foos_modern(const std::vector<FooBar>& foobars){
std::vector<Foo> valid_foos;
std::copy_if(foobars.begin(), foobars.end(), std::back_inserter(valid_foos),
[](const auto& foobar){
return foobar.is_valid;
});
//?? std::copy requires input and output types to match
return valid_foos;
}