简体   繁体   中英

Does variadic template function call lambda parameter in reverse order?

The below demo prints 21 instead of 12 . Why?

#include <iostream>
template<class... F> void callMany(F... fib){
    [&](...){}(
        (fib(),123)... 
    );
}
int main()
{
    auto f1=[&](){std::cout<<"1";};
    auto f2=[&](){std::cout<<"2";};
    callMany(f1,f2);
}

I tested it with both vc++ and g++. Both result are the same.

Is it a standard behavior? If so, which rules, and why?
Does it depend on compiler?

There is a way to reverse it, but I think it is a bit unrelated.

When an expression that contains a pack expansion is evaluated, the pack is expanded first---which simply instantiates the code, so to speak, without performing any evaluation---then the resulting expression is evaluated according to the usual rules. Your expression

[&](...){}(
    (fib(),123)... 
);

is expanded into

[&](...){}(
    (f1(),123), (f2(),123)
);

and since function arguments are evaluated in an unspecified order, you cannot rely on f2 being called before f1 or vice versa.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM