Typesafe variadics in any position

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Feb 26 13:10:17 PST 2013


On Tue, Feb 26, 2013 at 09:59:22PM +0100, simendsjo wrote:
> On Tuesday, 26 February 2013 at 20:52:46 UTC, Steven Schveighoffer
> wrote:
> >On Tue, 26 Feb 2013 15:36:11 -0500, Andrej Mitrovic
> ><andrej.mitrovich at gmail.com> wrote:
> >
> >>On Tuesday, 26 February 2013 at 20:29:51 UTC, Steven
> >>Schveighoffer wrote:
[...]
> >>foo(string[] args..., string file = __FILE__, size_t line =
> >>__LINE__);
> >>
> >>Right now as soon as you introduce variadics you can forget about
> >>using __FILE__/__LINE__ at runtime (unless you resort to some
> >>inner-template tricks, but that only works in some cases methinks,
> >>probably not at runtime).
> >
> >Well, except that example wouldn't work :)
> >
> >This would though:
> >
> >foo(string[] args..., size_t line = __LINE__, string file =
> >__FILE__);
> >
> >Excellent use case though!
[...]
>
> or use a new struct
> struct SourcePos {
>   string file;
>   size_t line;
> }
> foo(string[] args..., SourcePos pos = SourcePos(__FILE__, __LINE__));

Excellent idea!!! Why didn't I think of this before... this would help
even with the current limitation on variadics, as it helps solve
ambiguity problems with overloaded functions:

	void func(string a, string file=__FILE__, int line=__LINE__);
	void func(string a, string b, string file=__FILE__, int line=__LINE__);

	func("a");
	func("a", "b"); // ambiguity error

Encapsulating it in SourcePos avoids the ambiguity problem.

But of course, having variadics support this would make it so much more
useful.


T

-- 
It is not the employer who pays the wages. Employers only handle the
money. It is the customer who pays the wages. -- Henry Ford


More information about the Digitalmars-d mailing list