Can I override assert macro?

Pages: 12
I have an idea about what might be happening. Maybe the windows compiler treats the excess parameter as a warning instead of an error like gcc does. So it just ignores the second parameter.

Do you get a warning when you compile?

Does the entire message print out on the terminal? Or just the first part (before the comma)?

It would also possibly explain why it doesn't work when you switch the order of the string literal and the test since then the first (really only) parameter would always be true, which is pointless for an assert.
When I compile using the comma operator, no warning even if the assert is not triggered as it should. No warning if the "parameters" are swapped or not.

And the entire message prints out, including the test condition and the text message.
It's extremely unlikely (impossible?) that it's the "comma operator". If it was then it would always be true (if the string literal is last). It's far more likely that it is interpreted as two parameters. Hopefully someone else will try it so we can get a second opinion.
Last edited on
It's a comma that seems to create two parameters when only one is allowed in the assert call, so I call it the comma operator.

Whether or not that is how VS interprets the usage, not my bailiwick or my pay-grade.

I saw cppreference using a comma in their macro, and went from there.

As was pointed out earlier VS has an non-standard overload for the macro that does take more than one parameter.
You seem very confused.
Let's just wait for the second opinion.
I'm not confused. That would require I care about the inner working details of how language features are implemented.

I don't.

OK, who the hell reported dutch? He might be over-the-top blunter than is needed from time to time, this time is wasn't, but damn.
Last edited on
The comma is an operator if and only if it is part of an expression; otherwise it is a separator.

The Microsoft compiler/preprocessor generates a warning (if warnings are not suppressed):
warning C4002: too many actual parameters for macro 'assert'
https://rextester.com/SUR83596

"The preprocessor collects the extra parameters but ignores them during macro expansion."
https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4002?view=vs-2019


This is also true everywhere; operators are always parts of expressions.

1
2
3
4
5
6
7
8
9
10
#include <cstring>

int main()
{
    char cstr[100] ;
    
    std::strcpy( cstr, "hello world!" ) ; // fine, the comma is a separator
    
    std::strcpy( ( cstr, "hello world!" ) ) ; // *** error (function requires 2 arguments), the comma is an operator
}

https://rextester.com/QRQVE93156
Topic archived. No new replies allowed.
Pages: 12