Scope of C libraries in C++ - <X.h> vs <cX>

c++, gcc, gnu

Solution

Stephan T. Lavavej, a member of the MSVC team, addresses the reality of this situation (and some of the refinements to the standard) in this comment on one of his blog postings (http://blogs.msdn.com/vcblog/archive/2008/08/28/the-mallocator.aspx#8904359):

> also, `<cstddef>`, `<cstdlib>`, and `std::size_t` etc should be used!

I used to be very careful about that. C++98 had a splendid dream wherein `<cfoo`> would declare everything within namespace std, and `<foo.h>` would include `<cfoo>` and then drag everything into the global namespace with using-declarations. (This is D.5 [depr.c.headers].)

This was ignored by lots of implementers (some of which had very little control over the C Standard Library headers). So, C++0x has been changed to match reality. As of the N2723 Working Paper, http://open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf , now `<cfoo>` is guaranteed to declare everything within namespace std, and may or may not declare things within the global namespace. `<foo.h>` is the opposite: it is guaranteed to declare everything within the global namespace, and may or may not declare things within namespace std.

In reality and in C++0x, including `<cfoo>` is no safeguard against everything getting declared in the global namespace anyways. That's why I'm ceasing to bother with `<cfoo>`.

This was Library Issue 456, http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#456 .

(C++0x still deprecates the `<foo.h>` headers from the C Standard Library, which is hilarious.)

I'm in 100% agreement with Lavavej, except I never tried to be very careful about using the `<cfoo>` style headers even when I first started using C++ - the standard C ones were just too ingrained - and there was never any real world problem using them (and apparently there was never any real world benefit to using the `<cfoo>` style headers).

Problem

The C++ Programming Language : Special Edition states on page 431 that... `For every header < X.h > defining part of the C standard library in the global namespace and also in namespace std, there is a header < cX > defining the same names in the std namespace only.` However, when I use C headers in the < cX > style, I don't need to qualify the namespace. For example... ``` #include <cmath> void f() { double var = sqrt( 17 ); } ``` This would compile fine. Even though the book says that using the < cX > header defines names in the std namespace only, you are allowed to use those names without qualifying the namespace. What am I missing here? P.S. Using the GNU.GCC compiler

Original source

Related problems