assert from <cassert>

M

Matthias Kaeppler

Hi,

why can I use assert from <cassert> without resolving the std::
namespace? E.g.:

#include <cassert>

int main()
{
assert(true); // shouldn't this be: std::assert(true) ?
}

This compiles. I thought in the <c****> headers the macros are undef'ed
and replaced in favor of inline functions which reside in the std
namespace? In this case my program shouldn't compile. I'm using g++ 3.3.5.
 
V

Victor Bazarov

Matthias said:
why can I use assert from <cassert> without resolving the std::
namespace? E.g.:

#include <cassert>

int main()
{
assert(true); // shouldn't this be: std::assert(true) ?
}

This compiles. I thought in the <c****> headers the macros are undef'ed
and replaced in favor of inline functions which reside in the std
namespace? In this case my program shouldn't compile. I'm using g++ 3.3.5.

'assert' is a macro, not a function.

V
 
P

Phlip

Matthias said:
why can I use assert from <cassert> without resolving the std::
namespace? E.g.:

#include <cassert>

int main()
{
assert(true); // shouldn't this be: std::assert(true) ?

It's a #define macro.

(You might get in the habit of reading those header files...)
 
M

Matthias Kaeppler

Victor said:
'assert' is a macro, not a function.

V

If it hasn't been replaced by an inline function (like many other
macros), what's the point of the <cassert> header?
 
V

Victor Bazarov

Matthias said:
If it hasn't been replaced by an inline function (like many other
macros), what's the point of the <cassert> header?

I don't understand the question, I guess. What do you mean "what's the
point"? There are no Standard C++ headers with .h in them. The existence
of old .h headers is a compatibility requirement and those features
(listed in Annex D, BTW) have been deprecated. Now, things that are
macros in C are still macros in C++, see 17.4.1.2/5. The C++ Standard
_allows_ their implementation as functions but doesn't *mandate* it.

V
 
M

Matthias Kaeppler

Victor said:
I don't understand the question, I guess. What do you mean "what's the
point"? There are no Standard C++ headers with .h in them. The existence
of old .h headers is a compatibility requirement and those features
(listed in Annex D, BTW) have been deprecated. Now, things that are
macros in C are still macros in C++, see 17.4.1.2/5. The C++ Standard
_allows_ their implementation as functions but doesn't *mandate* it.

V

Oh, okay. It's just that I had a look at some of those c-headers a while
ago, and I spotted tons of #undef's and macros replaced by inline
functions. That's why I thought assert would now be an inline function, too.
 
M

Markus Moll

Hi

Matthias said:
Oh, okay. It's just that I had a look at some of those c-headers a while
ago, and I spotted tons of #undef's and macros replaced by inline
functions. That's why I thought assert would now be an inline function,
too.

If you can think of some way how an inline function could be able to print
messages like "Assertion failed in line 7, file 'test.cc': 1 == 4" then I
think it will soon become an inline function. Until then it will stay a
macro.

Markus
 
M

Matthias Kaeppler

Markus said:
Hi

Matthias Kaeppler wrote:




If you can think of some way how an inline function could be able to print
messages like "Assertion failed in line 7, file 'test.cc': 1 == 4" then I
think it will soon become an inline function. Until then it will stay a
macro.

Markus

Good point :)
 
J

James Aguilar

Matthias Kaeppler said:
Hi,

why can I use assert from <cassert> without resolving the std:: namespace?
E.g.:

#include <cassert>

int main()
{
assert(true); // shouldn't this be: std::assert(true) ?
}

This compiles. I thought in the <c****> headers the macros are undef'ed and
replaced in favor of inline functions which reside in the std namespace? In
this case my program shouldn't compile. I'm using g++ 3.3.5.

AFAIK, you don't have to use the std namespace for any of the <c*> headers.
This is true at least for cmath, cassert, and ctime.

- JFA1
 
V

Victor Bazarov

James said:
AFAIK, you don't have to use the std namespace for any of the <c*> headers.
This is true at least for cmath, cassert, and ctime.

Actually you K incorrectly. All C library functions are declared inside
the 'std' namespace if you include the <c*> headers. If your compiler
doesn't do that and allows you to keep using unqualified names, it's
broken. Get a better one.

V
 
P

Phlip

James said:
AFAIK, you don't have to use the std namespace for any of the <c*> headers.
This is true at least for cmath, cassert, and ctime.

That's the purpose of the <c*> headers - to improve the interface between
C++ and the legacy C Standard Library.

Naturally, many implementations compile a wide variety of sloppy
intermediate situations between the old styles and the new.
 
J

Jerry Coffin

Matthias Kaeppler wrote:

[ ... ]
Oh, okay. It's just that I had a look at some of those c-headers a
while ago, and I spotted tons of #undef's and macros replaced by
inline functions. That's why I thought assert would now be an
inline function, too.

assert expands to different things depending on whether NDEBUG is
defined at the point of expansion -- and you're allowed to #define and
#undef NDEBUG as you see fit throughout a translation unit. For
example:

#define NDEBUG 1
assert(x==1);
#undef NDEBUG
assert(y==2);

In this case, the first and second are required to expand differently
-- the first expands to something like '((void)0)', while the second
actually does an assertion.
 
P

Pete Becker

Victor said:
Actually you K incorrectly. All C library functions are declared inside
the 'std' namespace if you include the <c*> headers. If your compiler
doesn't do that and allows you to keep using unqualified names, it's
broken. Get a better one.

That's what the standard requires, but in practice it's unimplementable.
The problem is that the C++ standard effectively requires that the C
headers be rewritten to match C++'s rules, and that just won't fly. So
what you usually get is that the c* headers pull the C names into the
global namespace by including the C header, then hoist them into std
with using directives. There's an open defect report that would change
the standard to match reality: the c* headers give you names in std (and
maybe in the global namespace), the C headers give you names in the
global namespace (and maybe in std).
 
P

Pete Becker

Victor said:
Now, things that are
macros in C are still macros in C++, see 17.4.1.2/5. The C++ Standard
_allows_ their implementation as functions but doesn't *mandate* it.

It requires them to be macros. If they weren't, you wouldn't know
whether to write offsetof or std::eek:ffsetof. It also prohibits using
macros to mask functions, for the same reason (and you don't need those
macros in C++ because you can get the equivalent with inline).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Stop using assert macro 8
Place Assert in Exception 19
puzzles with abort() and assert() 11
assert() 4
Chatbot 0
more portable compile-time assert() 12
To assert or not to assert... 79
Custom ASSERT macro 7

Members online

Forum statistics

Threads
474,291
Messages
2,571,493
Members
48,160
Latest member
KieranKisc

Latest Threads

Top