digitalmars.D.learn - How to move append to an array?
- Yuxuan Shui (9/9) May 15 2017 Suppose I have a
- Stanislav Blinov (4/13) May 15 2017 moveEmplace is for moving an initialized object into an
- Yuxuan Shui (3/21) May 15 2017 Can I expand an array with uninitialized object? Or can I rely on
- Stanislav Blinov (5/7) May 15 2017 Built-in arrays always default-initialize their elements. If you
- Yuxuan Shui (2/10) May 15 2017 I just wish ~= could take moved objects.
- biocyberman (18/27) May 18 2017 Judging form the way you write the struct. It is of C/C++ style.
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
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
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:Can I expand an array with uninitialized object? Or can I rely on the compiler to optimize the initialization away?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
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
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:I just wish ~= could take moved objects.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
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