This question already has an answer here:
- Undefined behavior and sequence points 4 answers
I have a code that does similar to the one below, I compiled my code with g++-4.8 and it seems like the compiler is messing up the order of input arguments in the constructor when I call next on constructor.
#include <list>
#include <string>
#include <iostream>
std::string next( std::list<std::string> & l)
{
auto f = l.front();
l.pop_front();
return f;
}
struct A
{
explicit A(std::string a_, std::string b_) : a {std::move(a_)}, b { std::move(b_) }
{
std::cout << "a = " << a << " and b = " << b << "\n";
}
std::string a, b;
};
int main()
{
std::list<std::string> my_list { "one", "two", "three", "four" };
A a{ next(my_list), next(my_list) };
auto arg1 = next(my_list);
auto arg2 = next(my_list);
A a2 { arg1, arg2 };
}
the results of this code are as follow:
a = two and b = one
a = three and b = four
but they should be (and they are in g++7 nd g++-6)
a = one and b = two
a = three and b = four
I don't have this issue with g++-7 or 6, so I was wondering if it is simply just a compiler bug or g++-4.8 treats move differently and I am just not aware of it?
Aucun commentaire:
Enregistrer un commentaire