Specifying @nogc on structs seems to have no effect

Mike Parker aldacron at gmail.com
Tue Sep 19 13:59:31 UTC 2017

On Tuesday, 19 September 2017 at 13:46:20 UTC, Craig Black wrote:

> Thanks, I didn't know you could to that but it still doesn't 
> give me the behavior that I want:
> class Foo
> {
> }
> struct MyStruct
> {
> @nogc:
> public:
>   Foo foo; // This does not produce an error, but it still 
> requires a GC scan
>   void Bar()
>   {
>     foo = new Foo; // This produces an error
>   }
> }

@nogc applies to functions, not to types or variables. It 
prevents actions that allocate, like calls to new, appending to 
or concatenating arrays, and so on, inside the annotated 
function. By extension, no sort of GC scanning or collections 
will occur in such functions because that sort of thing can only 
happen during an allocation.

More information about the Digitalmars-d mailing list