I have problems to compile the following small code fragment. Visual Studio 2015 has problems in the deduction of the type of remover
. Can someone explain me why and how to fix this error?
My idea was to create a reuseable function deleting the first occurence of a value in a given STL container under some user definable predicate.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
#include <vector>
template<typename T>
auto removeOnlyOnce = [&](std::vector<T>& v, const std::function<bool(const T&)>& pred) {
auto iter = std::find_if(v.begin(), v.end(), pred);
if (iter != v.end()) {
v.erase(iter);
}
};
int main(int argc, char** args) {
std::vector<int> v{ 1,2,3,4,2,2,3 };
// Works
std::function<bool(const int&)> isTwoPred = [](int x) { return x == 2; };
removeOnlyOnce<int>(v, isTwoPred);
// Also works
removeOnlyOnce<int>(v, [](const int x)->bool { return x == 2; });
// Gives compile error: C3538
// auto remover = std::bind(removeOnlyOnce<int>, v, std::placeholders::_1);
// remover([](const int x)->bool { return true; });
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
}
Aucun commentaire:
Enregistrer un commentaire