Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 01, 2015 Capturing __FILE__ and __LINE in a variadic templated function | ||||
---|---|---|---|---|
| ||||
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 Re: Capturing __FILE__ and __LINE in a variadic templated function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 Re: Capturing __FILE__ and __LINE in a variadic templated function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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 Re: Capturing __FILE__ and __LINE in a variadic templated function | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | 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 Re: Capturing __FILE__ and __LINE in a variadic templated function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 Re: Capturing __FILE__ and __LINE in a variadic templated function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gary Willoughby | 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 Re: Capturing __FILE__ and __LINE in a variadic templated function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 Re: Capturing __FILE__ and __LINE in a variadic templated function | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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 Re: Capturing __FILE__ and __LINE in a variadic templated function | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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 Re: Capturing __FILE__ and __LINE in a variadic templated function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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
|
Copyright © 1999-2021 by the D Language Foundation