On 2012-10-03 20:23, timotheecour wrote:

Yes, that hack consists in :
1) defining extern(C) void std_stdio_static_this() instead of static this()
2) defining an auxiliary module std.stdiobase who will call it at module
construction:
module std.stdiobase;
extern(C) void std_stdio_static_this();
shared static this(){
     std_stdio_static_this();
}

I'm still puzzled as to why modifying the code inside
std_stdio_static_this (say with assert(0)) doesn't have any effect
(hence the need for the templated version I'm using).
Another workaround (besides adding std_stdio_static_this2) would be to
make the field File.p non-private so that my std_stdio_static_this2
could be defined outside std.stdio. Any suggestion?

What happens if you just call "std_stdio_static_this" the first you do in your main function?

Usually these kinds of problems are solved by using lazy initialization. In this case that would mean converting "stdin", "stdout" and "stderr" to functions.

https://github.com/D-Programming-Language/phobos/blob/master/std/stdio.d#L2357

Something like this:

private __gshared
{
    File _stdin;
    File _stdout;
    File _stderr;

    File.Impl stdinImpl;
    File.Impl stdoutImpl;
    File.Impl stderrImpl;
}

@property File stdin ()
{
    if (!_stdin.p)
        _stdin.p = &stdinImpl;

    return _stdin;
}

The same for "stdout" and "stderr". But this may not be good for performance reasons. I don't know how good the compiler is at optimizing these kind of functions.

--
/Jacob Carlborg

Reply via email to