The C++ Programming Language : Special Edition 在第 431 页指出...
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.
然而,当我在
#include <cmath>
void f() {
double var = sqrt( 17 );
}
这会很好地编译。尽管书中说使用
附言使用 GNU.GCC 编译器
最佳答案
Stephan T. Lavavej,MSVC 团队的一员,在他的一篇博文 (http://blogs.msdn.com/vcblog/archive/2008/08/28/the-mallocator.aspx#8904359) 的评论中解决了这种情况的现实情况(以及对标准的一些改进):
> also,
<cstddef>
,<cstdlib>
, andstd::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.)
我 100% 同意 Lavavej,除了我从未尝试过非常小心地使用 <cfoo>
样式的 header ,即使是在我第一次开始使用 C++ 时也是如此——标准的 C header 太根深蒂固了——而且从来没有真正的使用它们的世界问题(显然使用 <cfoo>
样式 header 从来没有任何现实世界的好处)。
关于c++ - C++ 中 C 库的范围 - <X.h> 与 <cX>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2118422/