Parameter pack expansion is reversed by the VS2015 compiler.
I have the following code:
#include <iostream>
#include <vector>
template <typename... T>
void f_Swallow(T &&...)
{
}
template <typename... T>
std::vector<int> f(T ...arg)
{
std::vector<int> result;
f_Swallow
(
[&]()
{
result.push_back(arg);
return true;
}
()...
) ;
return result;
}
using namespace std;
int main()
{
auto vec = f(1,2,3,4);
for (size_t i = 0; i < vec.size(); ++i)
cout << vec[i] << endl;
}
When I run this code in XCode (clang-700.1.81), I get this result:
1
2
3
4
But the same code run in VS2015 produces this output:
4
3
2
1
Why are the parameter packs expanded differently depending on the compiler? Is there a way to fix it without checking the platform and compiler version? Doesn't the standard guarantee anything about expansion order?
question from:https://stackoverflow.com/questions/35702180/why-does-parameter-pack-expansion-work-differently-with-different-c-compilers