I’m encountering a puzzling issue when using the assert macro in C++20 with std::map:
Code (Fails to Compile):
#include <bits/stdc++.h>
using namespace std;
int main() {
map<pair<int, int>, vector<int>> M0, M1;
// Assertion fails to compile
assert(M0[{1, 0}].size() == M1[{0, 1}].size());
return 0;
}
Error Message:
error: macro "assert" passed 3 arguments, but takes just 1
Code (Compiles Successfully,Wrap the conditional statements in assert with ()):
#include <bits/stdc++.h>
using namespace std;
int main() {
map<pair<int, int>, vector<int>> M0, M1;
// Assertion compiles successfully
assert((M0[{1, 0}].size() == M1[{0, 1}].size()));
return 0;
}
Does this mean that "assert" has a bug?
M0[{1, 0}].size() == M1[{0, 1}].size()and sees three text tokens:M0[{1and0}].size() == M1[{0and1}].size(), butassert(x)expects a single text token.