问题描述
因此,我发现我的C ++程序中需要libc.但是,我不喜欢将其散布在全局名称空间中的想法.理想情况下,我想将整个libc强制放入std::
命名空间,因此我必须执行std::memcpy
而不是memcpy
.
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
.
这可能吗?如何?我愿意在需要时使用特定于编译器的宏(我仅针对MS VC ++ 10.0和GCC 4.6).
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).
我的意思是强制将声明放入std"-因此,如果没有std ::前缀,它们将无法调用.另外,我包含的是cstdio
,而不是stdio.h
.
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
.
谢谢!
推荐答案
除非已完成,否则无法执行此操作.
You cannot do this, unless it's already done.
名称空间std
是保留给标准库的,禁止向该名称空间添加新成员.因此,如果C头尚未嵌入在std
中,那么您别无选择,只能接受它.
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.
另一方面,您可以完美地创建一个新的命名空间cstd
,并使用using
指令将全局命名空间中的符号引入其中...但不会使它们消失从全局名称空间访问.
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.
这篇关于C ++:如何强制将libc声明转换为std ::?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!