I’m assuming this is not possible because I got the following error:
error C3533: 'auto': a parameter cannot have a type that contains 'auto'
Here’s a code snippet to reproduce the error:
int myInts[] = {1,2,3,3,3,4};
std::vector<int> myVec(myInts, myInts + sizeof(myInts)/sizeof(int));
myVec.erase(
std::remove_if(myVec.begin(), myVec.end(),
[](auto i){return i==3;}), // lambda param error
myVec.end());
Now if you were to write this instead everything is fine and it will erase elements with the value of 3:
int myInts[] = {1,2,3,3,3,4};
std::vector<int> myVec(myInts, myInts + sizeof(myInts)/sizeof(int));
myVec.erase(
std::remove_if(myVec.begin(), myVec.end(),
[](int i){return i==3;}),
myVec.end());
So can you simply not use auto as a function parameter as the error suggests?
Is this because the type of auto is determined by the rvalue which the compiler can’t deduce despite it being a predicate of an algorithm executed on a known vector of int?
Does anyone know the reason?
Sadly, while this was suggested during the C++0x process, this ultimately never made it in. For simple functors, you may want to use something like Boost.Lambda (perhaps Phoenix v3 when it comes out, too), where the functors generated are polymorphic (and thus you don’t need to specify anything):
The solution with type inference only: