std.container.array.Array is not @nogc?

Jack Stouffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 15 04:49:52 PST 2017


On Sunday, 15 January 2017 at 11:47:06 UTC, drug007 wrote:
> Is there a way to use Array in @nogc code:
> ```
> import std.container.array : Array;
>
> @nogc:
> void main(string[ ] args)
> {
>    Array!int ai;
>    ai ~= 1;
>    assert(ai[0] == 1);
> }
> ```
> fails:
> ```
> main.d(8): Error: @nogc function 'D main' cannot call non- at nogc 
> function 'std.container.array.Array!int.Array.opOpAssign!("~", 
> int).opOpAssign'
>
>
> main.d(9): Error: @nogc function 'D main' cannot call non- at nogc 
> function 'std.container.array.Array!int.Array.opIndex'
> ```
> am I doing something wrong?

No you're not.

Array was designed before the @nogc attribute was created, so it 
wasn't coded with it's requirements in mind. Looking at the code, 
Array allocates GC memory for exception throwing in some cases. 
These can and should be changed to asserts.

I am writing a PR now to fix this. There doesn't seem to be too 
many cases to fix.


More information about the Digitalmars-d-learn mailing list