Equivalent of C++ #__VA_ARGS__

Ronoroa aravind.nujella at gmail.com
Sun Aug 2 16:58:12 UTC 2020


On Sunday, 2 August 2020 at 16:31:50 UTC, Adam D. Ruppe wrote:
> On Sunday, 2 August 2020 at 16:05:07 UTC, Ronoroa wrote:
>> That doesn't seem to stringize the args part like in 
>> #__VA_ARGS__
>
> oh yeah i missed that part.
>
> D basically can't do that exactly, but if you pass the args as 
> template things directly you can do this:
>
> ---
> void main(string[] args) {
>         int a;
>         int b = 34;
>         dbg!(a, b); // notice the !
> }
>
> void dbg(Args...)(size_t line = __LINE__) {
>         import std.stdio;
>
>         write("#", line, ": ");
>         foreach(idx, alias arg; Args) {
>                 if(idx) write(", ");
>                 write(arg.stringof, " = ", arg);
>         }
>         writeln();
> }
> ---
>
> #4: a = 0, b = 34
>
> 
> Or reformat the output however you want.

That's so much nicer than the C++ macro. Thanks a lot!




More information about the Digitalmars-d-learn mailing list