lundi 29 février 2016

Why c++ parameters packing works differently with different compilers?

C++ parameters pack expansion is reversed by VS2015 compiler.

I have the next 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'm running this code in XCode (clang-700.1.81), I get the next result:

1
2
3
4

But the same code, running in VS2015 produces the next output:

4
3
2
1

Why are the packing parameters expanded differently depending on the compiler, and if there someway to fix it without checking the platform and compiler version? Doesn't the standard guarantee anything about expanding order? Thank you for help.

Aucun commentaire:

Enregistrer un commentaire