digitalmars.D.learn - Equivalent of C++ #__VA_ARGS__
- Ronoroa (5/5) Aug 02 2020 How do I achieve equivalent semantics of following C++ code?
- Adam D. Ruppe (5/10) Aug 02 2020 You probably just want
- Ronoroa (15/19) Aug 02 2020 That doesn't seem to stringize the args part like in #__VA_ARGS__
- Adam D. Ruppe (22/24) Aug 02 2020 oh yeah i missed that part.
- Ronoroa (2/27) Aug 02 2020 That's so much nicer than the C++ macro. Thanks a lot!
How do I achieve equivalent semantics of following C++ code? ``` #define << print_func(__VA_ARGS__) << std::endl; ```
Aug 02 2020
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
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
On Sunday, 2 August 2020 at 16:05:07 UTC, Ronoroa wrote:That doesn't seem to stringize the args part like inoh 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
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's so much nicer than the C++ macro. Thanks a lot!That doesn't seem to stringize the args part like inoh 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