[Issue 8687] Variadic templates do not work properly with default arguments

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jun 22 00:22:19 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=8687


Andrej Mitrovic <andrej.mitrovich at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |k.hara.pg at gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich at gmail.com> 2013-06-22 00:22:18 PDT ---
A better use-case is:

void foo(Args...)(Args args, string file = __FILE__, size_t line = __LINE__)
{
}

This would allow us to construct error messages with file + line but without
creating template bloat because file and line usually have to become template
value arguments in order to support variadic arguments. IOW the above currently
has to be:

void foo(string file = __FILE__, size_t line = __LINE__, Args...)(Args args)
{
}

But this creates too much template bloat. I've seen other people ask for this
feature, I think schveiguy (Steven Schveighoffer) mentioned it as well.

Here's an example use-case, where an enforce-style function can take a format
string and avoid the need to call format() explicitly from the call-site:

/** Similar to $(D enforce), except it can take a formatting string as the
second argument. */
T require(T, Args...)
    (T value, lazy Args args, string file = __FILE__, size_t line = __LINE__)
{
    static if (Args.length)
        string msg = Args.length > 1 ? format(args[0], args[1 .. $]) :
text(args);
    else
        enum msg = "requirement failed.";

    if (!value)
        throw new Exception(msg, file, line);

    return value;
}

unittest
{
    string file = "foo.d";
    require(file.exists, "File '%s' does not exist.", file);
}

Currently this won't compile unless you make file and line compile-time
arguments.

@Kenji: Do you think it's possible to implement this without a big disruption
(e.g. code breakage)?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list