C library in C++ code

J

John Ratliff

For code that was part of the C library, being used in C++, should you
always prefix std::

Specific examples:

std::fopen
std::fclose
std::atexit

Should I include

<cstdio> or <cstdlib>?

g++ 3.4.2 in mingw doesn't seem to care one way or the other. No
warnings or errors are produced even with -W -Wall.

Thanks,

--John Ratliff
 
I

Ian

John said:
For code that was part of the C library, being used in C++, should you
always prefix std::

Specific examples:

std::fopen
std::fclose
std::atexit

Should I include

<cstdio> or <cstdlib>?

g++ 3.4.2 in mingw doesn't seem to care one way or the other. No
warnings or errors are produced even with -W -Wall.
One probably includes the other.

Ian
 
J

John Ratliff

One probably includes the other.

Ian

Sorry, my post was unclear.

I meant should I include cstdio for std::fclose/std::fopen/std::freopen
and should I include cstdlib for std::atexit.

I wasn't including either, and still no warnings. I was including
iostream, vector, and stdexcept.

--John Ratliff
 
M

Marc Mutz

John Ratliff wrote:
I wasn't including either, and still no warnings. I was
including iostream, vector, and stdexcept.
<snip>

In C++, unlike in C, the standard doesn't specify which
header files include each other. So always err on the
safe side and include headers for all functions/classes
you use in a translation unit. Specifically, don't rely
on <iostream> including <cstdio>.

Marc
 
R

Richard Herring

John Ratliff said:
For code that was part of the C library, being used in C++, should you
always prefix std::

Assuming you're using #include <cxxx> rather than #include <xxx.h>, you
*should*, but some implementations hoist those declarations into the
global namespace willy-nilly, so you might get away without the prefix.
But the first time you try to compile using something that doesn't,
you're in for a lot of editing :-(
Specific examples:

std::fopen
std::fclose
std::atexit

Should I include

<cstdio> or <cstdlib>?

You *should* include both, but in some implementations one will include
the other in any case. Again, you can't rely on this.
 
J

John Ratliff

Richard said:
Assuming you're using #include <cxxx> rather than #include <xxx.h>, you
*should*, but some implementations hoist those declarations into the
global namespace willy-nilly, so you might get away without the prefix.
But the first time you try to compile using something that doesn't,
you're in for a lot of editing :-(



You *should* include both, but in some implementations one will include
the other in any case. Again, you can't rely on this.

Thanks, I've done both. And yes, I don't want to rely on any specific
implementation.

--John Ratliff
 

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

Members online

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top