-
-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for multiple comma-separated arguments? #2
Comments
Could we reopen this issue since the commit is reverted? Quote from #8 (comment)
I think multiple arguments is a more common use case than returning from expressions. I have a similar debugging macro locally and I use it with multiple args from day to day. If we would like to support them at the same time, there are some possible routes:
|
Good point. I think the
I think I would go with the first option and return |
I second the case that multiple arguments is very useful. I had already used this widely before the change was reverted, so for now I'm stuck on the old version of A third option would be to return the last value, similar to how the comma operator works. |
I actually like that even better, just return the last argument. |
Unfortunately, this request has to be closed. There is no clean way to implement multiple argument support without losing another feature: unprotected commas within the dbg(MyClass{1,2,3}); or dbg(my_template_function<int, 2>()); wouldn't work anymore. Even if those things would still work with additional parenthesis ( For a detailed discussion, see #64. |
Yes there is no perfect solution here, but I would lean to support multiple arguments instead of unprotected commas:
|
Fair points. Opening this once again 😄
well, the workaround is to |
Catch2 is also a counter-example. For all of its one-argument macros ( |
Ah sure.
That's why I link to the CAPTURE() macro directly :P Catch2 even parses / splits the expressions in the CAPTURE() macro by itself, so it can handle some of the unprotected commas. Note that because C++ is a context sensitive, thus it's quite hard (if not impossible) to disambiguate code like Another hybrid approach is making output as |
Hm. I don't quite understand. How would this work? |
Here is a minimal proof-of-concept: #include <iostream>
#include <utility>
template <class T, class... U>
void dbg_impl(T &&head, U &&... tail) {
std::cerr << head;
if constexpr (sizeof...(U) > 0) {
std::cerr << ", ";
dbg_impl(std::forward<U>(tail)...);
} else {
std::cerr << "\n";
}
}
#define dbg(...) \
do { \
std::cerr << #__VA_ARGS__ << " = "; \
dbg_impl(__VA_ARGS__); \
} while (false)
int main() {
int x = 1;
int y = 2;
dbg(x, y, std::pair<int, int>(x, y).first);
return 0;
} It will output:
|
@ShikChen If we can make this work with the type-output as well (and pass all the tests that are currently in place), I think that would be a great compromise. |
Came here to say this, but @ShikChen beat me to it. |
Okay, so you are voting to drop the "commas inside dbg(…)" feature in favor of multiple arguments? I think I'm now also inclined to do so. In this case, I would prefer the solution where |
Note that there is an implementation of multiple arguments here: #8. It was reverted because it did not return the value, but I guess that could be fixed easily. Not sure if there are other implementations that would need less preprocessor code. |
I have a simple proof of concept, and I think we need to decide what's the expected behavior when multiple arguments are used with the special helpers that don't print the type and expressions, such as Currently there are 3 special cases we
Here is an example: #include <dbg.h>
#define NAME "bar"
typedef uint64_t u64;
int main() {
dbg("foo");
dbg(NAME);
dbg(dbg::time());
dbg(dbg::type<u64>());
// dbg(dbg::time(), 1 + 2, "meow"); // won't compile in the current version
return 0;
} Current output is:
I personally prefer to output:
Does it look ok to you? (I also prefer to shorten the type info as |
That sounds great!
Why? My preferred way how multiple arguments would work would be that the output of
I'm pretty sure that wouldn't work because
Yeah, let's discuss this in separate tickets. |
The text was updated successfully, but these errors were encountered: