forward variadic arguments

Deewiant deewiant.doesnotlike.spam at
Thu Jun 15 09:35:52 PDT 2006

Frank Benoit wrote:
>> void execSql(char[] aSql, ...) {
>> 	char[] sql;
>> 	void putc(dchar c) {
>> 		sql ~= c;
>> 	}
>> 	std.format.doFormat(&putc, _arguments, _argptr);
>> 	database.exec(sql);
>> }
> And what happend to my format string 'aSql' ? :)
> TypeInfo[] ti;
> ti ~= aSql.typeinfo;
> ti ~= _arguments
> void* p = ????;
> std.format.doFormat(&putc, ti, p);

Oh, whoops! :-)

I guess you could try a hack like that. I think you'll have to use std.stdarg -
somehow; I've never done this myself - to collect the arguments themselves into
an array and then pass the address of the first element of that array as the
equivalent of _argptr. BTW, "aSql.typeinfo" is deprecated: use
"typeid(typeof(aSql))" or just "typeid(char[])" if you're confident you'll never
change its type.

Personally, I'd just change the function signature to "void execSql(...)". It
gives a bit more versatility, the following are equivalent:

execSql("%d hello %d", a, b);
execSql(a, " hello ", b);
execSql("%d hello ", a, b);

If you really want to force the first parameter to be char[], I think the only
way to be sure (apart from that hackish approach, of course :-P) is to assert it
at runtime:

void execSql(...)
in {
	assert (_arguments[0] == typeid(char[]));
} body {
	// the code

More information about the Digitalmars-d-learn mailing list