Debug prints in @nogc
Johannes Pfau via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Aug 30 04:52:21 PDT 2016
Am Tue, 30 Aug 2016 10:26:28 +0000
schrieb Nordlöw <per.nordlow at gmail.com>:
> I'm struggling with debug printing in my @nogc-containers.
>
> The alternatives:
>
> assert(false, "Fixed message with no parameters");
>
> is not enough for my needs
>
> debug writeln("Fixed");
>
> doesn't bypass @nogc checking. Why?
>
> And temporary commenting out @nogc is cumbersome.
>
> I'm aware of C-style printf but that is not as flexible as
> writeln.
>
> Any advice?
-----------------------------------------------------
import std.stdio;
debug
{
enum writelnPtr = &writeln!string;
enum void function(string) @nogc writelnNoGC =
cast(void function(string) @nogc)writelnPtr;
}
void main() @nogc
{
debug writelnNoGC("foo");
}
-----------------------------------------------------
As long as it's only for debugging, the extra indirection shouldn't
matter for performance. Even for release builds the optimizer can
probably remove the indirection.
An alternative solution is using mangleof + pragma(mangle) to refer to
the external function.
In both cases this approach can be tedious for templated methods. You
don't want to write that boilerplate for every possible type
combination. However, it should be possible to refactor the code above
into a template and automate the boilerplate generation in some way.
More information about the Digitalmars-d-learn
mailing list