C ++: How to force libc declarations in std ::?

So, I am in need of libc in my C ++ program. However, I don't like the idea of ​​sprinkling it all over the global namespace. Ideally, I would like to force all libc into the std:: , so I would have to do std::memcpy , not memcpy .

Is it possible? And How? I want to use compiler-specific macros if necessary (I am only targeting MS VC ++ 10.0 and GCC 4.6).

Edit: I literally mean "force declare to std" so that they cannot be processed without the std :: prefix. In addition, I include cstdio , not stdio.h .

Thanks!

+4
source share
4 answers

You cannot do this if it is already done.

The std reserved for the standard library; it is forbidden to add new members to this namespace. Therefore, if the C headers are not yet embedded in std , then you have no choice but to accept it.

On the other hand, you can ideally create a new cstd namespace and transfer characters from the global namespace to it using using directives ... but this will not make them disappear from the global namespace.

+1
source

I literally mean "force declare to std" so that they are unrecoverable without the std prefix ::

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

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

+1
source

Make shell include

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

If the linker hates this, try simply declaring the functions you want:

 namespace std{ extern "C" { int memcpy( void *out, const void *in); } } 
0
source

Why do some (most?) C ++ compilers have C functions in the global namespace, they just have to use the existing functions of the operating system. For example, file functions may not be a separate C library, but OS file processing.

0
source

Source: https://habr.com/ru/post/1338581/


All Articles