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

Unbelievable

30 views
Skip to first unread message

Bonita Montero

unread,
Dec 10, 2019, 5:28:27 AM12/10/19
to
#include <iostream>
#include <algorithm>

using namespace std;

struct Inner
{
Inner() = default;
Inner( Inner &&other )
{
cout << "Inner( Inner &&other )" << endl;
}
Inner &operator =( Inner &&other )
{
cout << "Inner &operator =( Inner &&other )" << endl;
return *this;
}
};

struct Outer
{
Inner i;
};

int main()
{
Outer o1, o2;
swap( o1, o2 );
}

The Inner-object of Outer is moved although there is no move-constructor
or move-assignment-operator of Outer. Can anyone tell me where the C++
-standard does specifify this?

Paavo Helde

unread,
Dec 10, 2019, 5:40:08 AM12/10/19
to
15.8.1/8 [class.copy.ctor]

"If the definition of a class X does not explicitly declare a move
constructor, a non-explicit one will be implicitly declared as defaulted
if and only if
(8.1) — X does not have a user-declared copy constructor,
(8.2) — X does not have a user-declared copy assignment operator,
(8.3) — X does not have a user-declared move assignment operator, and
(8.4) — X does not have a user-declared destructor.
"

Your struct Outer matches all these points.

Bonita Montero

unread,
Dec 10, 2019, 7:44:22 AM12/10/19
to
> 15.8.1/8 [class.copy.ctor]
> "If the definition of a class X does not explicitly declare a move
> constructor, a non-explicit one will be implicitly declared as defaulted
> if and only if
> (8.1) — X does not have a user-declared copy constructor,
> (8.2) — X does not have a user-declared copy assignment operator,
> (8.3) — X does not have a user-declared move assignment operator, and
> (8.4) — X does not have a user-declared destructor.
> "
> Your struct Outer matches all these points.

That's what I thought about an hour after posting.

0 new messages