I was really bothered by the inclusion of C stdlib functions on the global namespace and ended up writing things like ::snprintf or ::errno or struct ::stat, etc, to differentiate from some of my own functions in the enclosing namespace where those c stdlib functions were used.
Then I discovered that there is a way to declare every C stdlib function in the std namespace (as STL): just include < c(lib) > instead of < (lib).h > so I’ve edited my code the use those new “c for c++” includes.
On Debian/GCC 4.3.4 I had to 2 problems:
1) #error This file requires compiler and library support for the upcoming \
ISO C++ standard, C++0x. This support is currently experimental, and must be \
enabled with the -std=c++0x or -std=gnu++0x compiler options.
2) using -std=c++0x my program compiles just fine, but I have not modified ::snprintf or ::time, etc.. every C stdlib function is still on the global namespace =(! (no, I’m not using namespace std not even once)
Any thoughts?
For example.. how to stop the c stdlib from invading my global namespace? < c(lib) > is an experimental feature of the next C++ standard or could be used safely right now?
Then I’ve another doubt that perhaps deserves a new question.. there is no cmalloc. I know the whole history about new replacing malloc and why. but for simple plain byte buffers there is no c++ equivalent of realloc. I know that memory blocks and reallocation are implementation/so specific, but when there are contiguous free blocks of memory realloc works better than a new buffer allocation and memory copy.
Thanks =)!
For your first question, it depends on which headers you are trying to include. Most of the C headers are available in the
c(lib)form in the existing version of C++. A few aren’t, and may be added in C++0x. So if you tried to include any of those, you might have gotten that error.Second, all the headers of this form guarantee that the functions will be made available in the
stdnamespace. But they do not promise to leave the global namespace alone. Often, they put the symbols in both namespaces.I’m not sure why
::snprintfbothers you more thanstd::snprintfthough. You have to specify a prefix in both cases.As for
realloc, a C++ equivalent doesn’t exist, probably because it’s more trouble than it’s worth, especially with C++’s more complicated semantics for copying objects. (In particular, if you try to use it, don’t store any non-POD objects in the buffer, asreallocwill justmemcpythem to a newly allocated buffer if necessary, which will break non-POD objects.)Of course, you can still use the old
reallocfrom C by including its header. But I’d say you’re probably better off using new/delete, and simply figuring out a sensible buffer allocation strategy.