www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Equivalent of C++ #__VA_ARGS__

reply Ronoroa <aravind.nujella gmail.com> writes:
How do I achieve equivalent semantics of following C++ code?

```
#define
<< print_func(__VA_ARGS__) << std::endl;
```
Aug 02 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 2 August 2020 at 15:30:27 UTC, Ronoroa wrote:
 How do I achieve equivalent semantics of following C++ code?

 ```
 #define
 << print_func(__VA_ARGS__) << std::endl;
 ```
You probably just want void dbg(Args...)(Args args, size_t line = __LINE__) { writeln(line, args, " = ", print_func(args)); }
Aug 02 2020
parent reply Ronoroa <aravind.nujella gmail.com> writes:
On Sunday, 2 August 2020 at 15:48:34 UTC, Adam D. Ruppe wrote:
 On Sunday, 2 August 2020 at 15:30:27 UTC, Ronoroa wrote:
 void dbg(Args...)(Args args, size_t line = __LINE__) {
      writeln(line, args, " = ", print_func(args));
 }
I've tried void dbg(Args...)(Args args, size_t line = __LINE__) { // print_func is simply writeln writeln(args); } void main() { int a = 10, b = 4; // ^^^^ this dbg(a, b); }
Aug 02 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 2 August 2020 at 16:05:07 UTC, Ronoroa wrote:
 That doesn't seem to stringize the args part like in 

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; foreach(idx, alias arg; Args) { if(idx) write(", "); write(arg.stringof, " = ", arg); } writeln(); } --- Or reformat the output however you want.
Aug 02 2020
parent Ronoroa <aravind.nujella gmail.com> writes:
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 

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; foreach(idx, alias arg; Args) { if(idx) write(", "); write(arg.stringof, " = ", arg); } writeln(); } --- Or reformat the output however you want.
That's so much nicer than the C++ macro. Thanks a lot!
Aug 02 2020