jeudi 20 décembre 2018

Parameter Pack Iterating Backwards when evaluating function in argument list

I am learning parameter packing in C++ and am confused by the output of this snippet:

template<typename... Ts>
void f1(Ts... args)
{
    std::cout << "------------------------" << std::endl;
    int dummy[sizeof...(Ts)] = { (std::cout << args << std::endl, 0)... };
    std::cout << "------------------------" << std::endl;
}

template<typename Tout, typename T>
Tout f2(T arg)
{
    std::cout << arg << std::endl;
    return 2.0 * (Tout)arg;
}

template<typename... Ts>
void f3(Ts... args)
{
    f1(f2<double>(args)...);
}

int main()
{
    f1(1, 2, 3);    
    f3(1, 2, 3);
}

The output on my machine is shown below, with a highlight of the important lines

------------------------
1
2
3
------------------------
3   <<< GOING BACKWARDS >>>
2
1
------------------------
2
4
6
------------------------

It appears to me that the evaluation of f2<double>(args)... inside of f3 is going backwards, but then flips around for the execution of f1. From the reading I've done so far, I thought that parameter packing was guaranteed to be in order, so this is very surprising.

This is a minimal example of a larger project, in which it is actually important that f2 be executed in order. Is there some way I can make it not reverse the order, or, at the very least, am I guaranteed to always have it go backwards?

Aucun commentaire:

Enregistrer un commentaire