Thread overview
How to move append to an array?
5 days ago
biocyberman
May 15
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
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 16
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 16
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 16
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.
5 days ago
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.