1

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?

cpplearner
  • 13,776
  • 2
  • 47
  • 72
superjax
  • 252
  • 2
  • 9
  • 2
    the order of evaluation for function parameters is unspecified: https://stackoverflow.com/questions/2934904/order-of-evaluation-in-c-function-parameters – kmdreko Dec 21 '18 at 00:44
  • Ah, That makes sense. Thank you. – superjax Dec 21 '18 at 00:50
  • 3
    Possible duplicate of [Order of evaluation in C++ function parameters](https://stackoverflow.com/questions/2934904/order-of-evaluation-in-c-function-parameters) – 1201ProgramAlarm Dec 21 '18 at 01:04

0 Answers0