By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

assert from <cassert>

P: n/a
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.

--
Matthias Kaeppler
Jul 23 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Matthias Kaeppler wrote:
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
Jul 23 '05 #2

P: n/a
Matthias Kaeppler wrote:
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...)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #3

P: n/a
Victor Bazarov wrote:
Matthias Kaeppler wrote:
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


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

--
Matthias Kaeppler
Jul 23 '05 #4

P: n/a
Matthias Kaeppler wrote:
Victor Bazarov wrote:
Matthias Kaeppler wrote:
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

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
Jul 23 '05 #5

P: n/a
Victor Bazarov wrote:
Matthias Kaeppler wrote:
Victor Bazarov wrote:
Matthias Kaeppler wrote:

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


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


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.

--
Matthias Kaeppler
Jul 23 '05 #6

P: n/a
Hi

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.


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
Jul 23 '05 #7

P: n/a
Markus Moll wrote:
Hi

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.

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 :)

--
Matthias Kaeppler
Jul 23 '05 #8

P: n/a

"Matthias Kaeppler" <no****@digitalraid.com> wrote in message
news:d3*************@news.t-online.com...
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
Jul 23 '05 #9

P: n/a
James Aguilar wrote:
"Matthias Kaeppler" <no****@digitalraid.com> wrote in message
news:d3*************@news.t-online.com...
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.


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
Jul 23 '05 #10

P: n/a
James Aguilar wrote:
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.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #11

P: n/a
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #12

P: n/a
Victor Bazarov wrote:

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).

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #13

P: n/a
Victor Bazarov wrote:
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::offsetof. 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).

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.