I use this define in c++ to debug my variables it works in clion and codeblocks but does not work in visual studio 22.
Error C2010 ‘.’: unexpected in macro parameter list codeforces
the file name is "codeforces"
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
I use this code below instead but it’s not functional if there is alot of variables since i’ll need to copy-paste is alot
#define error(s) cout << #s << " = " << s << endl;
How can I fix this problem ?
2
Answers
The correct preprocessor syntax would be
...
for the parameter and#__VA_ARGS__
for its expansion:Another, not so macro intense, version could be to pass on the stringified
#__VA_ARGS__
to a C++ function template. I made a pre- C++20 and a C++20 version for that here and here’s the C++20 demo.As others have pointed out,
args...
is not standard C++. To capture everything in the parentheses of a macro, simply use...
and then refer to it through__VA_ARGS__
.See GCC Variadic Macros, and cppreference: Replacing text macros.
Besides that, you should also minimize the use of macros as much as possible.
You only need the macro so that you can call
do_error
and give it the string representation of the passed expression at the same time.The rest can just be a variadic function template:
Also note that your
err
function template can be improved: