6

So, I find myself in the need of libc in my C++ program. However, I do not like the idea of sprinkling it all over the global namespace. Ideally, I'd like to force the entirety of libc into the std:: namespace so I'd have to do std::memcpy rather than memcpy.

Is this possible? And how? I'm willing to use compiler-specific macros if needed (I target only MS VC++ 10.0 and GCC 4.6).

Edit: I do literally mean 'force the declarations into std' - so that they are uncallable without the std:: prefix. Also, I am including cstdio, not stdio.h.

Thanks!

  • 3
    *"I do literally mean 'force the declarations into std' - so that they are uncallable without the std:: prefix."* The programmer who comes after you may not appreciate this. – dmckee --- ex-moderator kitten Feb 05 '11 at 20:31
  • It's an open source project with established coding conventions. I could write a thesis on why contributors should not be altering code style, but this isn't the place... –  Feb 05 '11 at 20:35
  • 3
    In theory, when you include the `` headers, all the non-macro entities are only supposed to be included in `std` and not in the global namespace. In practice, most implementations don't do that, and the next C++ standard, C++0x, has changed the rules to allow the `` headers to put names into the global namespace as well. – James McNellis Feb 05 '11 at 20:36
  • 2
    Seems to me that they're making it worse... But no matter, it is what it is. –  Feb 05 '11 at 20:47
  • 4
    I agree that it's worse, but it's kind of useless to impose rules that no one follows :-| – James McNellis Feb 05 '11 at 21:10

4 Answers4

1

I do literally mean 'force the declarations into std' - so that they are uncallable without the std:: prefix.

You can't do this if your implementation exposes the names in the global namespace. You can use the <cXXX> headers and then use std:: yourself.

This is, perhaps, unfortunate, but it is a consequence of C compatibility, since C does not understand namespaces. C++ has traditionally maintained many kludges and sacrifices for C compatibility.

Fred Nurk
  • 13,952
  • 4
  • 37
  • 63
1

You cannot do this, unless it's already done.

The namespace std is reserved to the Standard Library, it is forbidden to add new members to this namespace. Therefore, if the C-headers are not already embedded within std, then you have no choice but to accept it.

On the other hand, you can perfectly create a new namespace, cstd, and bring the symbols from the global namespace in it with using directives... but it won't make them disappear from the global namespace.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
0

Make wrapper includes

//stdlib.hpp
namespace std
{
#include <stdlib.h> //edit: changed from cstdlib to stdlib.h
}

If the linker hates this try just declaring the functions you want:

namespace std{ extern "C" {

int memcpy( void *out, const void *in);
} }
KitsuneYMG
  • 12,753
  • 4
  • 37
  • 58
  • 1
    They are already in the `std` namespace. Also, the MS VC++ headers add `using std::` for every single libc function, so this won't help. Not sure if GCC does the same. –  Feb 05 '11 at 20:26
  • The using won't do crap because it's wrapped in another std namespace. What happens is namespace std{ namespace std{ ...} using std::foo }. The linker probably wouldn't like it, except it will be able to find the symbol `std::memcpy` in the crt libs. There may be an issue if It forces itself to look for `std::std::memcpy` – KitsuneYMG Feb 05 '11 at 20:29
  • Point taken. This works excellently on MS VC++, however, not on GCC 4.1.2: http://paste2.org/p/1230554 –  Feb 05 '11 at 20:41
  • 1
    This is very likely to break or otherwise simply do the wrong thing. – Fred Nurk Feb 05 '11 at 20:50
  • Ok. Try using `#include ` instead of `cstdio`. The _lib_.h files do not have the namespace stuff in them. It works in gcc 4.2.1 – KitsuneYMG Feb 05 '11 at 21:25
  • The edit which wraps in namespace std is also very likely to break or otherwise simply do the wrong thing. – Fred Nurk Feb 05 '11 at 22:26
0

The reason some (most?) C++ compilers have the C functions in the global namespace, is simply that they have to use the existing operating system functions. For example, the file functions might not be a separate C library, but the file handling of the OS.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203