Jump to page: 1 2
Thread overview
Capturing __FILE__ and __LINE in a variadic templated function
Nov 01, 2015
Nordlöw
Nov 01, 2015
Adam D. Ruppe
Nov 01, 2015
anonymous
Nov 02, 2015
Nordlöw
Nov 02, 2015
Gary Willoughby
Nov 02, 2015
Nordlöw
Nov 02, 2015
John Colvin
Nov 02, 2015
Daniel N
Nov 02, 2015
Nordlöw
Nov 02, 2015
Daniel N
Nov 02, 2015
Nordlöw
Nov 03, 2015
Jonathan M Davis
Nov 03, 2015
Nordlöw
Nov 03, 2015
Nordlöw
Nov 03, 2015
Jonathan M Davis
November 01, 2015
Is it possible to make the following definition

void show(alias T, string file = __FILE__, uint line = __LINE__, string fun = __FUNCTION__)()
{
    import std.stdio: writeln;
    try { debug writeln(file, ":",line, ":" /* , ": in ",fun */, " debug: ", T.stringof, ":", T); }
    catch (Exception) { }
}

used as

unittest
{
    int x = 11;
    int y = 12;
    int z = 13;
    show!x;
    show!y;
    show!z;
}

variadic so that it, instead, can be called as

unittest
{
    int x = 11;
    int y = 12;
    int z = 13;
    show!(x,y,z);
}

and still capture current file, line and function?
November 01, 2015
Yeah, just make the other args normal runtime instead of template:

void show(T...)(string file = __FILE__, uint line = __LINE__, string fun = __FUNCTION__) {
  // same body
}

// same usage
November 01, 2015
On 01.11.2015 23:49, Adam D. Ruppe wrote:
> Yeah, just make the other args normal runtime instead of template:

Or make it two nested templates:

template show(T ...)
{
    void show(string file = __FILE__, uint line = __LINE__,
        string fun = __FUNCTION__)()
    {
        ...
    }
}
November 02, 2015
On Sunday, 1 November 2015 at 23:36:15 UTC, anonymous wrote:
> On 01.11.2015 23:49, Adam D. Ruppe wrote:
>> Yeah, just make the other args normal runtime instead of template:
>
> Or make it two nested templates:
>
> template show(T...)
> {
>     void show(string file = __FILE__, uint line = __LINE__,
>         string fun = __FUNCTION__)()
>     {
>         ...
>     }
> }

Doesn't work.

I need `T` to be an alias in order for .stringof to work.

How do I specify a variadic template argument alias list of aliases?
November 02, 2015
On Monday, 2 November 2015 at 08:23:16 UTC, Nordlöw wrote:
> I need `T` to be an alias in order for .stringof to work.

typeof(T).stringof
November 02, 2015
On Monday, 2 November 2015 at 09:02:28 UTC, Gary Willoughby wrote:
> On Monday, 2 November 2015 at 08:23:16 UTC, Nordlöw wrote:
>> I need `T` to be an alias in order for .stringof to work.
>
> typeof(T).stringof

No, I want the variable name from the calling scope.

This works for a single argument.

void show(alias arg, string file = __FILE__, uint line = __LINE__, string fun = __FUNCTION__)()
{
    import std.stdio: writeln;
    try
    {
        debug writeln(file, ":",line, ":" /* , ": in ",fun */, " debug: ", arg.stringof, " is ", arg);
    }
    catch (Exception) { }
}

unittest
{
    int x = 11;
    show!x;
}

Prints

dbg.d:80: debug: x is 11

My try at variadic

template show(Args...)
{
    void show(string file = __FILE__, uint line = __LINE__, string fun = __FUNCTION__)()
    {
        import std.stdio: write, writeln;
        try
        {
            debug write(file, ":",line, ":" /* , ": in ",fun */, " debug: ");
            foreach (const i, Arg; Args)
            {
                if (i) debug write(", "); // separator
                debug write(Arg.stringof, " is ", Arg);
            }
            debug writeln();
        }
        catch (Exception) { }
    }
}

fails with compilation error

dbg.d(83,5): Error: variable x cannot be read at compile time

Why can't I make Args a sequence of aliases?
November 02, 2015
On Monday, 2 November 2015 at 09:16:09 UTC, Nordlöw wrote:
> On Monday, 2 November 2015 at 09:02:28 UTC, Gary Willoughby wrote:
>> On Monday, 2 November 2015 at 08:23:16 UTC, Nordlöw wrote:
>>> I need `T` to be an alias in order for .stringof to work.
>>
>> typeof(T).stringof
>
> No, I want the variable name from the calling scope.
>
> This works for a single argument.
>
> void show(alias arg, string file = __FILE__, uint line = __LINE__, string fun = __FUNCTION__)()
> {
>     import std.stdio: writeln;
>     try
>     {
>         debug writeln(file, ":",line, ":" /* , ": in ",fun */, " debug: ", arg.stringof, " is ", arg);
>     }
>     catch (Exception) { }
> }
>
> unittest
> {
>     int x = 11;
>     show!x;
> }
>
> Prints
>
> dbg.d:80: debug: x is 11
>
> My try at variadic
>
> template show(Args...)
> {
>     void show(string file = __FILE__, uint line = __LINE__, string fun = __FUNCTION__)()
>     {
>         import std.stdio: write, writeln;
>         try
>         {
>             debug write(file, ":",line, ":" /* , ": in ",fun */, " debug: ");
>             foreach (const i, Arg; Args)
>             {
>                 if (i) debug write(", "); // separator
>                 debug write(Arg.stringof, " is ", Arg);
>             }
>             debug writeln();
>         }
>         catch (Exception) { }
>     }
> }
>
> fails with compilation error
>
> dbg.d(83,5): Error: variable x cannot be read at compile time
>
> Why can't I make Args a sequence of aliases?

Works for me on multiple compilers. To be precise, this worked:

template show(Args...)
{
    void show(string file = __FILE__, uint line = __LINE__, string fun = __FUNCTION__)()
    {
        import std.stdio: write, writeln;
        try
        {
            debug write(file, ":",line, ":" /* , ": in ",fun */, " debug: ");
            foreach (const i, Arg; Args)
                {
                if (i) debug write(", "); // separator
                debug write(Arg.stringof, " is ", Arg);
            }
            debug writeln();
        }
        catch (Exception) { }
    }
}

unittest
{
    int x = 11;
    show!x;
}

November 02, 2015
On Monday, 2 November 2015 at 09:54:50 UTC, John Colvin wrote:
>> Why can't I make Args a sequence of aliases?
>
> Works for me on multiple compilers. To be precise, this worked:
>

Except it prints Arg instead of x, try:
debug write(Args[i].stringof, " is ", Arg);

November 02, 2015
On Monday, 2 November 2015 at 09:54:50 UTC, John Colvin wrote:
> Works for me on multiple compilers. To be precise, this worked:
>
> template show(Args...)
> {
>     void show(string file = __FILE__, uint line = __LINE__, string fun = __FUNCTION__)()
>     {
>         import std.stdio: write, writeln;
>         try
>         {
>             debug write(file, ":",line, ":" /* , ": in ",fun */, " debug: ");
>             foreach (const i, Arg; Args)
>                 {
>                 if (i) debug write(", "); // separator
>                 debug write(Arg.stringof, " is ", Arg);
>             }
>             debug writeln();
>         }
>         catch (Exception) { }
>     }
> }
>
> unittest
> {
>     int x = 11;
>     show!x;
> }

No. This prints:

    Arg is 11

I want it to print the name of Arg in the closing as

    x is 11

equivalent to what my single-parameter overload

void show(alias arg, string file = __FILE__, uint line = __LINE__, string fun = __FUNCTION__)()
{
    import std.stdio: writeln;
    try
    {
        debug writeln(file, ":",line, ":" /* , ": in ",fun */, " debug: ", arg.stringof, " is ", arg);
    }
    catch (Exception) { }
}

does.
November 02, 2015
On Monday, 2 November 2015 at 11:36:27 UTC, Nordlöw wrote:
> I want it to print the name of Arg in the closing as
>
>     x is 11

See my previous comment:
Arg -> Args[i].stringof

« First   ‹ Prev
1 2