I wrote a template that takes an istream&
and a function and is supposed to extract all parameters for this function from the istream
, call the function with these parameters and return the result. It all works fine, except for the order of evaluation of function parameters. See code, further details and final question below:
#include <iostream>
#include <vector>
void Foo(int i, std::string s)
{
std::cout << "input was " << i << " and " << s << '\n';
}
template<typename T>
T Parse(std::istream &s)
{
T res;
s >> res;
return res;
}
template<typename TR, typename ... TArgs>
TR Bar(std::istream &s, TR f(TArgs...) )
{
return f(Parse<TArgs>(s)...);
}
int main()
{
Bar(std::cin, Foo);
}
Input:
1 2
Expected output:
input was 1 and 2
Actual output:
input was 2 and 1
I know that the evaluation of function parameters is implementation specific and obviously here the last parameter got evaluated first and read the first input.
How do I to fix this code and force a specific order of evaluation on the parameters? Maybe evaluate them seperately before calling the function? Is it even possible without violating the standard and/or relying on a specific implementation or compiler?
For example, we can use an intermediate
std::tuple
to force the order of evaluation:In contrast to function arguments, the order of evaluation of arguments in a braced list is fixed by their order in that list, [dcl.init.list/4]: