#include <algorithm>
#include <list>
#include <vector>
template <typename ContainerOut, typename ContainerIn>
ContainerOut KeepNegatives(const ContainerIn& xs)
{
ContainerOut result;
auto itOut = std::inserter(result, std::end(result));
auto isNegative = [](auto x){ return x < 0; };
std::copy_if(std::begin(xs), std::end(xs), itOut, isNegative);
return result;
}
int main()
{
typedef std::vector<int> IntVector;
typedef std::list<int> IntList;
IntVector intVector = { 1, -2, -3, 4 };
IntList intList = { 1, -2, -3, 4 };
auto intVec2 = KeepNegatives<IntVector>(intList);
auto intList2 = KeepNegatives<IntList>(intVector);
auto intVec3 = KeepNegatives<IntVector>(intVector);
auto intVec4 = KeepNegatives(intVector);
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGxpc3Q+CiNpbmNsdWRlIDx2ZWN0b3I+Cgp0ZW1wbGF0ZSA8dHlwZW5hbWUgQ29udGFpbmVyT3V0LCB0eXBlbmFtZSBDb250YWluZXJJbj4KQ29udGFpbmVyT3V0IEtlZXBOZWdhdGl2ZXMoY29uc3QgQ29udGFpbmVySW4mIHhzKQp7CiAgICBDb250YWluZXJPdXQgcmVzdWx0OwogICAgYXV0byBpdE91dCA9IHN0ZDo6aW5zZXJ0ZXIocmVzdWx0LCBzdGQ6OmVuZChyZXN1bHQpKTsKICAgIGF1dG8gaXNOZWdhdGl2ZSA9IFtdKGF1dG8geCl7IHJldHVybiB4IDwgMDsgfTsKICAgIHN0ZDo6Y29weV9pZihzdGQ6OmJlZ2luKHhzKSwgc3RkOjplbmQoeHMpLCBpdE91dCwgaXNOZWdhdGl2ZSk7CiAgICByZXR1cm4gcmVzdWx0Owp9CgppbnQgbWFpbigpCnsKICAgIHR5cGVkZWYgc3RkOjp2ZWN0b3I8aW50PiBJbnRWZWN0b3I7CiAgICB0eXBlZGVmIHN0ZDo6bGlzdDxpbnQ+IEludExpc3Q7CiAgICAKICAgIEludFZlY3RvciBpbnRWZWN0b3IgPSB7IDEsIC0yLCAtMywgNCB9OwogICAgSW50TGlzdCBpbnRMaXN0ID0geyAxLCAtMiwgLTMsIDQgfTsKCiAgICBhdXRvIGludFZlYzIgPSBLZWVwTmVnYXRpdmVzPEludFZlY3Rvcj4oaW50TGlzdCk7CiAgICBhdXRvIGludExpc3QyID0gS2VlcE5lZ2F0aXZlczxJbnRMaXN0PihpbnRWZWN0b3IpOwogICAgYXV0byBpbnRWZWMzID0gS2VlcE5lZ2F0aXZlczxJbnRWZWN0b3I+KGludFZlY3Rvcik7CgogICAgYXV0byBpbnRWZWM0ID0gS2VlcE5lZ2F0aXZlcyhpbnRWZWN0b3IpOwp9
prog.cpp: In function 'int main()':
prog.cpp:27:43: error: no matching function for call to 'KeepNegatives(IntVector&)'
auto intVec4 = KeepNegatives(intVector);
^
prog.cpp:6:14: note: candidate: template<class ContainerOut, class ContainerIn> ContainerOut KeepNegatives(const ContainerIn&)
ContainerOut KeepNegatives(const ContainerIn& xs)
^
prog.cpp:6:14: note: template argument deduction/substitution failed:
prog.cpp:27:43: note: couldn't deduce template parameter 'ContainerOut'
auto intVec4 = KeepNegatives(intVector);
^