Skip to main content
GameDev.net gamedev.net
🔒 Locked

default move constructor only copies member array elements

Started by yacwroy Nov 21, 2010 at 12:55 AM 1 replies 1.2k views
Original Post
yacwroy
yacwroy
Is this intended?

#include <iostream>struct CFoo  {    CFoo() =default;    CFoo(const CFoo&)      { std::cout << "copy" << std::endl; }        CFoo(CFoo&&)      { std::cout << "move" << std::endl; }  };struct CBar  {    CBar() =default;    CBar(const CBar&) =default;    CBar(CBar&&) =default;        CFoo foo[2];    CFoo f;  };int main()  {    CBar b = std::move(CBar());  };
Output:
copycopymove
(Compiled with GCC 4.6)

Shouldn't the foo[] array members be moved, just like the regular member? or is this correct?
I have a workaround if it's correct, but it just seems kind-of inconsistent.

Thanks.
Nitage
Nitage
As far as I can tell from the draft standard, the array members should be moved:

Quote:

§ 12.8.17:
The implicitly-defined move constructor for a non-union class X performs a memberwise move of its subobjects.
...
Each subobject is copied in the manner appropriate to its type:
...
— if the subobject is an array, each element is copied, in the manner appropriate to the element type;


The inplicit move constructor stuff is fairly new - it's very possibly a bug.

Edit:
I copied and pasted the wrong sentence. It should have been the sentence you posted "- an array is initialized by moving each element in the manner appropriate to the element type;"

[Edited by - Nitage on November 22, 2010 7:01:18 AM]
yacwroy
yacwroy
Ok thanks for pointing me in the right direction re the standard.

This might be from a later version of the draft (C++ std version N3126):
Quote:
§12.8.17 --4
an array is initialized by moving each element in the manner appropriate to the element type;


That seems fairly explicit. I submitted a bug to GCC.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.