best replacement for - cout << "hello D" << endl; ?
Bruce Adams
tortoise_74 at ya.dont.spam.me.hoo.co.uk
Wed Jul 11 23:42:31 PDT 2007
Jarrett Billingsley Wrote:
> "Bruce Adams" <tortoise_74 at ya.nos.pam.hoo.co.uk> wrote in message
> news:f7445a$1g7g$1 at digitalmars.com...
> > 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);
>
> writefln();
> }
>
> 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.
>
I believe this is exactly what I'm looking for.
I see this is actually in the example for variadic templates.
http://www.digitalmars.com/d/variadic-function-templates.html
Just one small problem. I can't get it to compile. At first I thought
you were using incomplete/pseudo code to demonstrate but now I'm guessing maybe the compiler I'm using is incomplete.
void myWritefln(T...)(T args) // line 7
{
foreach(arg; args) // line 9
writef("%s", arg);
writefln();
}
test.d:7: found '...' when expecting ')'
test.d:7: semicolon expected following function declaration
test.d:7: Declaration expected, not ')'
test.d:9: no identifier for declarator args
test.d:9: semicolon expected, not ')'
test.d:9: Declaration expected, not ')'
test.d:12: no identifier for declarator writefln
test.d:13: unrecognized declaration
F:\projects>gdc --version
gdc (GCC) 3.4.4 (cygming special, gdc 0.23, using dmd 1.007))
Before I post this as a bug can someone check I haven't made a noob error.
Regards,
Bruce.
More information about the Digitalmars-d-learn
mailing list