www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to move append to an array?

reply Yuxuan Shui <yshuiv7 gmail.com> writes:
Suppose I have a

struct A {
    disable this(this);
} x;

How do I append it into an array?

Do I have to do

array.length++;
moveEmplace(x, array[$-1]);

?
May 15 2017
next sibling parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Monday, 15 May 2017 at 21:38:52 UTC, Yuxuan Shui wrote:
 Suppose I have a

 struct A {
    disable this(this);
 } x;

 How do I append it into an array?

 Do I have to do

 array.length++;
 moveEmplace(x, array[$-1]);

 ?
moveEmplace is for moving an initialized object into an uninitialized one. Use the two-argument move() function: move(x, array[$-1]);
May 15 2017
parent reply Yuxuan Shui <yshuiv7 gmail.com> writes:
On Monday, 15 May 2017 at 23:36:06 UTC, Stanislav Blinov wrote:
 On Monday, 15 May 2017 at 21:38:52 UTC, Yuxuan Shui wrote:
 Suppose I have a

 struct A {
    disable this(this);
 } x;

 How do I append it into an array?

 Do I have to do

 array.length++;
 moveEmplace(x, array[$-1]);

 ?
moveEmplace is for moving an initialized object into an uninitialized one. Use the two-argument move() function: move(x, array[$-1]);
Can I expand an array with uninitialized object? Or can I rely on the compiler to optimize the initialization away?
May 15 2017
parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Tuesday, 16 May 2017 at 01:22:49 UTC, Yuxuan Shui wrote:

 Can I expand an array with uninitialized object? Or can I rely 
 on the compiler to optimize the initialization away?
Built-in arrays always default-initialize their elements. If you need something that unsafe, there's std.array.uninitializedArray: http://dlang.org/phobos/std_array.html#uninitializedArray What are you trying to achieve?
May 15 2017
parent Yuxuan Shui <yshuiv7 gmail.com> writes:
On Tuesday, 16 May 2017 at 01:34:50 UTC, Stanislav Blinov wrote:
 On Tuesday, 16 May 2017 at 01:22:49 UTC, Yuxuan Shui wrote:

 Can I expand an array with uninitialized object? Or can I rely 
 on the compiler to optimize the initialization away?
Built-in arrays always default-initialize their elements. If you need something that unsafe, there's std.array.uninitializedArray: http://dlang.org/phobos/std_array.html#uninitializedArray What are you trying to achieve?
I just wish ~= could take moved objects.
May 15 2017
prev sibling parent biocyberman <biocyberman gmail.com> writes:
On Monday, 15 May 2017 at 21:38:52 UTC, Yuxuan Shui wrote:
 Suppose I have a

 struct A {
    disable this(this);
 } x;

 How do I append it into an array?

 Do I have to do

 array.length++;
 moveEmplace(x, array[$-1]);

 ?
Judging form the way you write the struct. It is of C/C++ style. With that said, it's not clear what you are trying to do. There is a basic reference about array here: http://dlang.org/spec/arrays.html And this works: cat arrayappend.d // arrayappend.d content unittest { auto a = [1, 2]; a ~= 3; assert( a == [1, 2, 3]); } // Finish content Running test: rdmd -unittest -main arrayappend.d No error message means the test passes.
May 18 2017