Sign in to follow this  
ordered_disorder

What version of assert should I use, and CPPUNIT

Recommended Posts

AFAIK there is no std::assert. Should I just use MSDN's version that came with my compiler? Or is there a better verseion out there? Also, has anyone here ever used cppunit?. I think I am good programmer in most areas, except that of bug testing! I am trying to become better, thus I am researching many methods.

Share this post


Link to post
Share on other sites
There is no std::assert because assert is implemented as a macro, not a function. It's one of the uglinesses of C++ that things like that can't be done well without reverting to the features of C which are supposedly deprecated. Don't worry, though; the assert() that ships with MSVC is totally standard.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
It's one of the uglinesses of C++ that things like that can't be done well without reverting to the features of C which are supposedly deprecated.


Which feature does the assert macro use that is "supposedly deprecated"? I don't think assert or its equivalent can be done in any compiled language without using a macro facility. You need something to convert the expression to a string, and something to provide the file and line numbers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Adam Hamilton
You could make an assert method. This has worked for me :)


One of the features of the macro assert() is that it is completely removed in release mode, guaranteeing zero overhead. An assert function will always pay the cost of evaluating the expression (unless the compiler is very smart, or the expression is very simple).

Quote:
Original post by JohnBolton
I don't think assert or its equivalent can be done in any compiled language without using a macro facility.


C#'s Debug.Assert() gets away with it without using macros, but some other sort of attribute magic :)

Quote:
Or is there a better verseion out there?

For 'normal' applications I've found the default MSVC assert() perfectly fine, but it can cause lockups if you're doing anything tricky. I often define my own as something like

#define assert(X) if (X) {} else __asm int 3

so that it acts like a simple breakpoint, but sometimes something more complicated is required if you're trying to debug a full screen 3D game.

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
Which feature does the assert macro use that is "supposedly deprecated"? I don't think assert or its equivalent can be done in any compiled language without using a macro facility. You need something to convert the expression to a string, and something to provide the file and line numbers.

Of course. But it flies in the face of the oft-repeated assertion that you shouldn't use #define in C++, doncha think?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this