how to call std_stdio_static_this from a dynamically loaded shared library (osx)

Jacob Carlborg doob at me.com
Wed Oct 3 23:35:47 PDT 2012


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

> In my case I can't: I don't have control over the main function (it
> could be written in C for example).

If you're developing a library or similar you could have a requirement 
that the user need to call "Library.initialize()" before use. But of 
course, that's something anyone would like to avoid.

> I guess you also want to add inside the if(): "stdinImpl.handle =
> core.stdc.stdio.stdin;", etc.

Right, I missed that.

> I doubt the compiler can do much optimizing here, since the if won't
> always be true, but probably IO is slow enough compared to a branch so
> it might not matter.
>
> That being said, templatizing std_stdio_static_this as above would avoid
> branching at every single call to writeXX functions. Would there be any
> disadvantage?

I have no idea. I would guess as long as "std_stdio_static_this" or the 
corresponding code is run before std.stdio is used you should be fine. 
You have to be careful here about threads, you don't want to have a 
thread you don't know about sneaking up behind your back and starts 
using std.stdio before it's initialized.

> Also, I grepped for static_this, it appears this hack is only used in 2
> places:
> * std.stdio (=>stdiobase)
> * std.process (=>processinit)
> So the same template trick could be used in those 2 places.

You can write this instead:

void std_stdio_static_this2 () ()

Note the extra pair of empty parentheses. This will force the function 
to be a template. I think this look slightly better.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list