Thread overview |
---|
September 16, 2015 any way to initialize an array of structs to void? | ||||
---|---|---|---|---|
| ||||
struct MyStruct { @disable this(); this(int a, string b) { this.a = a; this.b = b; } int a; string b; } I know there is a way to create one instance of `MyStruct` and initialize it to void. MyStruct s = void; s = MyStruct(5, "abcdef"); How can initialize an array of `MyStruct` instances to void? auto arr = new MyStruct[10]; // compile-time Error: default construction is disabled for type MyStruct. |
September 16, 2015 Re: any way to initialize an array of structs to void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ref2401 | On Wednesday, 16 September 2015 at 14:45:06 UTC, ref2401 wrote:
> struct MyStruct {
> @disable this();
>
> this(int a, string b) {
> this.a = a;
> this.b = b;
> }
>
> int a;
> string b;
> }
>
> I know there is a way to create one instance of `MyStruct` and initialize it to void.
> MyStruct s = void;
> s = MyStruct(5, "abcdef");
>
> How can initialize an array of `MyStruct` instances to void?
> auto arr = new MyStruct[10]; // compile-time Error: default construction is disabled for type MyStruct.
MyStruct[10] arr = void;
Don't do this with a dynamic array, though, as they work a bit differently from static arrays.
|
September 16, 2015 Re: any way to initialize an array of structs to void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Wednesday, 16 September 2015 at 14:48:59 UTC, Meta wrote:
> Don't do this with a dynamic array, though, as they work a bit differently from static arrays.
Unfortunately I have to deal with dynamic arrays.
|
September 16, 2015 Re: any way to initialize an array of structs to void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ref2401 | On Wednesday, 16 September 2015 at 14:51:51 UTC, ref2401 wrote: > On Wednesday, 16 September 2015 at 14:48:59 UTC, Meta wrote: > >> Don't do this with a dynamic array, though, as they work a bit differently from static arrays. > > Unfortunately I have to deal with dynamic arrays. In that case, you can use std.array.uninitializedArray or std.array.minimallyInitializedArray as needed. http://dlang.org/phobos/std_array.html#uninitializedArray http://dlang.org/phobos/std_array.html#.minimallyInitializedArray |
September 16, 2015 Re: any way to initialize an array of structs to void? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Wednesday, 16 September 2015 at 14:57:49 UTC, Meta wrote:
>
> In that case, you can use std.array.uninitializedArray or std.array.minimallyInitializedArray as needed.
>
> http://dlang.org/phobos/std_array.html#uninitializedArray
> http://dlang.org/phobos/std_array.html#.minimallyInitializedArray
Thanks)
|
Copyright © 1999-2021 by the D Language Foundation