Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 02, 2020 Equivalent of C++ #__VA_ARGS__ | ||||
---|---|---|---|---|
| ||||
How do I achieve equivalent semantics of following C++ code? ``` #define dbg(...) std::cout << __LINE__ << #__VA_ARGS__ << " = " << print_func(__VA_ARGS__) << std::endl; ``` |
August 02, 2020 Re: Equivalent of C++ #__VA_ARGS__ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ronoroa | On Sunday, 2 August 2020 at 15:30:27 UTC, Ronoroa wrote:
> How do I achieve equivalent semantics of following C++ code?
>
> ```
> #define dbg(...) std::cout << __LINE__ << #__VA_ARGS__ << " = " << 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));
}
|
August 02, 2020 Re: Equivalent of C++ #__VA_ARGS__ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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));
> }
That doesn't seem to stringize the args part like in #__VA_ARGS__
I've tried
void dbg(Args...)(Args args, size_t line = __LINE__) {
writeln("#", line, ":", args, " = ");
// print_func is simply writeln
writeln(args);
}
void main() {
int a = 10, b = 4;
// gives #11 : 104 = 104
// what I crucially want #9 : a, b = 10, 4
// ^^^^ this
dbg(a, b);
}
|
August 02, 2020 Re: Equivalent of C++ #__VA_ARGS__ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ronoroa | 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.
|
August 02, 2020 Re: Equivalent of C++ #__VA_ARGS__ | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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!
|
Copyright © 1999-2021 by the D Language Foundation