Re: How to move append to an array?

2017-05-18 Thread biocyberman via Digitalmars-d-learn

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.



Re: How to move append to an array?

2017-05-15 Thread Yuxuan Shui via Digitalmars-d-learn

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.


Re: How to move append to an array?

2017-05-15 Thread Stanislav Blinov via Digitalmars-d-learn

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?


Re: How to move append to an array?

2017-05-15 Thread Yuxuan Shui via Digitalmars-d-learn

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?


Re: How to move append to an array?

2017-05-15 Thread Stanislav Blinov via Digitalmars-d-learn

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]);