开发者

Is std::remove_if with lambda predicate and auto element possible?

开发者 https://www.devze.com 2023-03-12 11:34 出处:网络
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\'

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):

std::remove_if(myVec.begin(), myVec.end(),
    _1 == 3)

The solution with type inference only:

// uses pass-by-reference unlike the question
std::remove_if(myVec.begin(), myVec.end(),
    [](decltype(myVec[0]) i){return i==3;})


An auto is a type inference based on the value you're initialising it to. A parameter isn't initialised to anything at the place it appears in the code.


Basically, this was already suggested, then rejected, and then lambdas were added in, so it very nearly made it in but happened not to, and is very likely to make it into the language in the future.

0

精彩评论

暂无评论...
验证码 换一张
取 消