473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

assert-macro: brackets

Leo
Hi @ all.

I looked up the implementation of the assert macro of my compiler
(MinGW), because I wanna write my own assert.

I found this:

#define assert(x) ((void)0)
#define assert(e) ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)
Jul 22 '05 #1
4 2027
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@email.com> wrote:
Hi @ all.

I looked up the implementation of the assert macro of my compiler
(MinGW), because I wanna write my own assert.

I found this:

#define assert(x) ((void)0)
#define assert(e) ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)


No, try the following with your two versions of assert

if (0 && assert(0))
{
}

With the first definition this will not call _assert (which is correct),
with the second definition it will.

john
Jul 22 '05 #2
John Harrison wrote:
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@email.com> wrote:
....
No, try the following with your two versions of assert

if (0 && assert(0))
I think you meant

if (0 && assert(1))
{
}

With the first definition this will not call _assert (which is
correct), with the second definition it will.


G
Jul 22 '05 #3
Leo
John Harrison wrote:
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@email.com> wrote:
Hi @ all.

I looked up the implementation of the assert macro of my compiler
(MinGW), because I wanna write my own assert.

I found this:

#define assert(x) ((void)0)
#define assert(e) ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)

No, try the following with your two versions of assert

if (0 && assert(0))
{
}

With the first definition this will not call _assert (which is
correct), with the second definition it will.

john


Both versions won't compile :D
Jul 22 '05 #4
On Sun, 25 Jul 2004 00:38:29 +0200, Leo <le*******@email.com> wrote:
John Harrison wrote:
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@email.com> wrote:
Hi @ all.

I looked up the implementation of the assert macro of my compiler
(MinGW), because I wanna write my own assert.

I found this:

#define assert(x) ((void)0)
#define assert(e) ((e) ? (void)0 : _assert(#e, __FILE__,
__LINE__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)

No, try the following with your two versions of assert
if (0 && assert(0))
{
}
With the first definition this will not call _assert (which is
correct), with the second definition it will.
john


Both versions won't compile :D


Yes, I realised that after I posted.

How about this?

1 && assert(0);

That fails to compile with version 1, but compiles with version 2.

I'm sure a better example could be devised, but the point is that the
extra bracket in the first version avoids any surprises.

john
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
3
by: Thomas Guettler | last post by:
Hi, Python 2.3.3 (#1, Feb 5 2005, 16:22:10) on linux2 >>> assert 0, "foo" Traceback (most recent call last): File "<stdin>", line 1, in ? AssertionError: foo >>> assert(0, "foo") >>>
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
5
by: Alex Vinokur | last post by:
Here are two programs. --- foo1.c --- #include <assert.h> #define FOO 10 int main() { assert (15 < FOO); return 0; }
47
by: Rob Thorpe | last post by:
In general, is it considered bad practice to use asserts in production code? What about writing a macro that does the same as assert but continues to work regardless of the state of NDEBUG? I...
2
by: cody | last post by:
System.Diagnostics.Debug.Assert(); Hello??? A language should encourage programmers to heavily use the assert-feature, since it improves safety, stability, readability and maintainability of...
13
by: priyanka | last post by:
Hi there, Can anyone show me how the assert() function works ? I need to develop my own assert() function instead of using the one defined in the assert.h file. It would be great if anyone could...
29
by: mailforpr | last post by:
Sometimes, I can't think of any good reason why I should have the program's logic thrown an exception. Except for catching the exception and printing "Uh, oh" to the screen. I also think that in...
9
by: pereges | last post by:
Ok, so once I'm done debugging my code(split across multiple modules) using the assert macro, I would want to switch off all the assert macros ued in the program. Does this mean I have to include:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.