Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document move constructor #3918

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions spec/struct.dd
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,156 @@ $(H3 $(LNAME2 implicit-copy-constructors, Implicit Copy Constructors))
$(P If the generated copy constructor fails to type check, it will receive the `@disable` attribute.)


$(H2 $(LEGACY_LNAME2 StructMoveConstructor, struct-move-constructor, Struct Move Constructors))

$(P Move constructors are used to initialize a `struct` instance from
another instance of the same type, and then the other instance is reset to its initial state.
This effects a move rather than a copy. A `struct` that defines a move constructor
is not $(RELATIVE_LINK2 POD, POD).)

$(P A constructor declaration is a move constructor declaration if it meets
the following requirements:)

$(UL
$(LI The `this` part of the declaration is preceded by an `=`, i.e. `=this` to distinguish
the move constructor from other constructors.)

$(LI It takes exactly one parameter without a
$(DDSUBLINK spec/function, function-default-args, default argument),
followed by any number of parameters with default arguments.)

$(LI Its first parameter is a $(B not)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$(LI Its first parameter is a $(B not)
$(LI Its first parameter is $(B not) a

$(DDSUBLINK spec/function, ref-params, `ref` parameter).)

$(LI The type of its first parameter is the same type as
$(DDSUBLINK spec/type, typeof, `typeof(this)`), optionally with one or more
$(DDLINK spec/const3, Type Qualifiers, type qualifiers) applied to it.)

$(LI It is not a
$(DDSUBLINK spec/template, template_ctors, template constructor declaration).)
)

---
struct A
{
=this(return scope A rhs) {} // move constructor
=this(return scope const A rhs, int b = 7) {} // move constructor with default parameter
}
---

$(P The move constructor is type checked as a normal constructor.)

$(P If a move constructor is defined, implicit calls to it will be inserted
in the following situations:)

$(OL
$(LI When an rvalue is used to initialize a variable:)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about A a = A(); - the RHS is an rvalue, but presumably the move constructor is not called?


$(SPEC_RUNNABLE_EXAMPLE_RUN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the SPEC_RUNNABLE_EXAMPLE_ code needs to be COMMENT'd out to pass DAutoTest:

__stdin.d(3): Error: declaration expected, not `=`

---
struct A
{
int[] arr;
=this(return scope A rhs) { arr = rhs.arr; rhs.arr = null; }
}

void main()
{
A a;
a.arr = [1, 2];

A b = __rvalue(a); // move constructor gets called
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is __rvalue implemented by the dmd pull?

b.arr[] += 1;
assert(a.arr is null); // a was reset to initial state
assert(b.arr == [2, 3]);
}
---
)

$(LI When a parameter is passed by value to a function:)

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
---
struct A
{
=this(return scope A another) {}
}

void fun(A a) {}

void main()
{
A a;
fun(__rvalue(a)); // move constructor gets called
}
---
)

$(LI When a parameter is returned by value from a function and Named Returned Value Optimization (NRVO)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor tweak to match https://dlang.org/spec/glossary.html#nrvo.

Suggested change
$(LI When a parameter is returned by value from a function and Named Returned Value Optimization (NRVO)
$(LI When a parameter is returned by value from a function and Named Return Value Optimization (NRVO)

cannot be performed:)

$(SPEC_RUNNABLE_EXAMPLE_COMPILE
---
struct A
{
=this(return scope A another) {}
}

A fun()
{
A a;
return a; // NRVO, no move constructor call
}

A a;
A gun()
{
return __rvalue(a); // cannot perform NRVO, a moved to return value
}

void main()
{
A a = fun();
A b = gun();
}
---
)
)

$(H3 $(LNAME2 disable-move, Disabled Moving))

$(P Move constructor attributes work analogously to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$(P Move constructor attributes work analogously to
$(P Disabling move construction works analogously to

$(LINK2 disable-copy, Disabled Copying).)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$(LINK2 disable-copy, Disabled Copying).)
$(RELATIVE_LINK2 disable-copy, Disabled Copying).)


$(H3 $(LNAME2 move-constructor-attributes, Move Constructor Attributes))

$(P Move constructor attributes work analogously to
$(LINK2 copy-constructor-attributes, Copy Constructor Attributes).)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$(LINK2 copy-constructor-attributes, Copy Constructor Attributes).)
$(RELATIVE_LINK2 copy-constructor-attributes, Copy Constructor Attributes).)


$(H3 $(LNAME2 implicit-move-constructors, Implicit Move Constructors))

$(P A move constructor is generated implicitly by the compiler for a `struct S`
if all of the following conditions are met:)

$(OL
$(LI `S` does not explicitly declare any move constructors;)
$(LI `S` defines at least one direct member that has a move constructor, and that
member is not overlapped (by means of `union`) with any other member.)
)

$(P If the restrictions above are met, the following move constructor is generated:)

---
=this(return scope inout(S) src) inout
{
foreach (i, ref inout field; src.tupleof)
this.tupleof[i] = field;
}
---

$(P If the generated move constructor fails to type check, it will receive the `@disable` attribute.)


$(H2 $(LEGACY_LNAME2 StructPostblit, struct-postblit, Struct Postblits))

$(GRAMMAR
Expand Down