Std :: move (std :: array) g ++ vs visual c ++
I am having a problem implementing the move constructor for an item in my std :: array in my project in visual studio 2013.
So, I tried to make a minimal example in notepad ++ which I compiled with g ++ 5.3.0.
Only to find that in g ++ I could do what I was trying
G ++ example:
#include <iostream>
#include <array>
using namespace std;
struct A{
A() = default;
A(const A&)
{
cout << "copy constructed" << endl;
}
A(A&&)
{
cout << "move constructed" << endl;
}
};
class B{
public:
B(array<A, 2>&& a)
: m_a(std::move(a))
{}
private:
array<A, 2> m_a;
};
int main(){
A foo;
cout << "=========1===========" << endl;
array<A, 2> a = { { foo, std::move(foo) } };
cout << "=========2===========" << endl;
B b(std::move(a));
cout << "=========3===========" << endl;
array<A, 2> a_second = std::move(a);
return 0;
}
Output:
========= 1 ===========
copy built
traffic built ========= 2 ============
traffic built traffic built ========= 3 ===========
Movement built Movement built
When I tried (practically) the same code in visual studio 2013, the result was different:
Output:
========= 1 ===========
copy built
movement built ========= 2 ===========
copy built
copy built
========= 3 ===========
copy built
copy built
How can I use the move constructor in visual C ++ and why is visual C ++ refusing to use it here?
source to share
This is a bug in MSVS 2013. MSVS 2013 does not generate implicit move constructors . If you run it in MSVS 2015 or 2017, you get the same result.
I would also like to point out that
B(array<A, 2>& a) : m_a(std::move(a))
This is not how you want to move the object in B
. If you want to B
capture an array you must have
B(array<A, 2>&& a) : m_a(std::move(a))
This means that instead of using
B b(a);
you must use
B b(std::move(a));
and now you can clearly see what a
was moved from main
.
source to share
Visual studio 2013 is not fully compatible with C ++ 11. Moving support for std containers is one of those "not fully implemented" parts. Your example works great for the latest VS2017, see Rextester .
PS Here you can get detailed information about support of C ++ features in different compilers.
source to share