best replacement for - cout << "hello D" << endl; ?

Jarrett Billingsley kb3ctd2 at
Wed Jul 11 20:10:13 PDT 2007

"Bruce Adams" <tortoise_74 at> wrote in message 
news:f7445a$1g7g$1 at
> I see. That makes quite a lot of difference. Now is there a way to remove 
> the format string entirely.
>  a) as a function
>  b) as a template that can be expanded at compile time.
> It sounds like its trivial to write a) if it doesn't already exist. 
> Hopefully, it does though.

Bill showed one way to do it with writefln, which still happens all at 
runtime.  Another way is with variadic templates:

void myWritefln(T...)(T args)
    foreach(arg; args)
        writef("%s", arg);


myWritefln("Hi! ", 5, " that's all.");

There are a few things to note about this:

1) That foreach loop in the function is actually run at compile time and 
unrolled; if you call this function with three arguments, the body will be 
repeated three times, once for each argument.

2) You don't necessarily have to use writef[ln]() on the inside; you could 
use this to wrap Tango-style (blah)(x) output in a more natural-looking 
function call.

3) The issue with this is code bloat -- an instance of this template is 
created for each separate list of parameter types.

(But ignoring the code bloat, it's just too damn cool of a feature not to 
use it ;) )

Furthermore some people (as has been mentioned in this thread) have been 
working on compile-time formatting which checks that the formatting strings 
match up, typewise, with their arguments. 

More information about the Digitalmars-d-learn mailing list