Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy and move the member object

57 views
Skip to first unread message

Pawel Por

unread,
Mar 27, 2023, 4:11:53 PM3/27/23
to
Hello,

Assume there is a struct with a single member object. When creating the object of a struct I want the member object sometimes be copied and sometimes moved. Is the following approach correct ? Assume I don't want to use generic programing.

#include <utility>

struct Item
{
Item() {}
Item(const Item&) {}
Item(Item&&) {}

Item& operator=(const Item&) { return *this; }
Item& operator=(Item &&) { return *this; }
};

struct Container
{
Item item;

Container(const Item& item) : item(item) {} // copying
Container(Item&& item) : item(std::move(item)) {} // moving
};


int main(int argc, char **argv)
{
Item a;

Container cont(a); // copying
Container cont2(std::move(a)); // moving

return 0;
}

Paavo Helde

unread,
Mar 28, 2023, 8:40:58 AM3/28/23
to
27.03.2023 23:11 Pawel Por kirjutas:

> struct Container
> {
> Item item;
>
> Container(const Item& item) : item(item) {} // copying
> Container(Item&& item) : item(std::move(item)) {} // moving
> };

Yes, that's about how it goes.

If anything, naming both the member and the constructor parameter with
the same name does not help readability (but works as intended, in the
member initialization list).

Öö Tiib

unread,
Mar 31, 2023, 6:39:39 AM3/31/23
to
On Monday, 27 March 2023 at 23:11:53 UTC+3, Pawel Por wrote:
> Hello,
>
> Assume there is a struct with a single member object. When creating the object of a struct I want the member object sometimes be copied and sometimes moved. Is the following approach correct ? Assume I don't want to use generic programing.
>
Note that destructor of Item is missing. In your example (that does nothing)
it does not matter, but in real class it should be always present when you
define any of assignment, move or copy construction. It is called "Rule of
zero, three or five".

Your Container lacks all of 5 (that is OK) but has implicit copy and move
conversion constructors from other class. That can cause confusion.

Perhaps do not use examples that do nothing. You may fail when you
attempt to write programs that do something.

Yes, your main() compiles but most compilers optimise all the code out of
it as it does nothing externally observable. IOW copying or moving has
only any point when there is something to copy or to move and that is used
for something meaningful. Note that also such code compiles (and does also
nothing, and so is as meaningless):

Item a;
Container cont = a;
Container cont2 = std::move(a);
cont = cont2;
cont = std::move(cont2);
cont2 = a;
cont2 = std::move(a);

Jorgen Grahn

unread,
Apr 8, 2023, 4:38:33 AM4/8/23
to
I'm more worried by the lack of "explicit". Given the names "Item" and
"Container", the OP probably doesn't want one to confuse them.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages