C ++库的范围 - < X.h> vs< cX> [英] Scope of C libraries in C++ - <X.h> vs <cX>

查看:87
本文介绍了C ++库的范围 - < X.h> vs< cX>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++编程语言:特殊版在第431页指出... ...
$ b $ p

对于每个标题< X.h>在全局命名空间中定义C标准库的一部分,并且也在命名空间std中定义一个头标< cX>只在std命名空间中定义相同的名字。



然而,当我在< cX>样式,我不需要限定命名空间。例如...

  #include  void f(){
double var = sqrt(17);
}

这样可以很好地编译。尽管这本书说使用< cX>头文件仅在std名称空间中定义名称,您可以使用这些名称而不必限定名称空间。我在这里错过了什么?



使用GNU.GCC编译器

解决方案

Stephan T. Lavavej是MSVC团队的成员,他解决了这种情况和一些对标准的改进)在他的博客文章( http://blogs.msdn.com/vcblog/archive/2008/08/28/the-mallocator.aspx#8904359 ):


>也是,< cstddef> < cstdlib> std :: size_t 等应该被使用!



我以前非常小心。 C ++ 98有一个灿烂的梦,其中< cfoo >将声明命名空间std中的所有内容,并且< foo.h> 将包含< cfoo> ,然后用using声明将全部内容拖入全局名称空间。 (这是D.5 [depr.c.headers]。)



很多实现者忽略了它们(其中一些对C标准库头)。所以,C ++ 0x已经改变为符合现实。截至N2723工作文件, http:// open- std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf ,现在< cfoo> 保证声明namespace std内的所有内容,并且可能会或可能不会在全局命名空间中声明事物。 < foo.h> 是相反的:它保证声明全局名称空间内的所有内容,并且可以或不可以在名称空间标准内声明。



实际上,在C ++ 0x中,包括< cfoo> 并不能防止在全局名称空间中声明的所有内容。这就是为什么我不再打扰< cfoo>



这是Library Issue 456, http://www.open-std.org/jtc1 / cs2 / wg21 / docs / lwg-defects.html #456



(C ++ 0x仍旧弃用<


我现在在foo.h> 来自C标准库的头文件,与Lavavej达成100%的协议,但我从来没有试过对使用< cfoo> 样式标题时非常小心,即使我刚开始使用C ++时 - 标准C只是根深蒂固 - 从来没有任何真正的世界问题使用它们(显然,使用< cfoo> 样式标题从来没有任何实际的好处)。


The C++ Programming Language : Special Edition states on page 431 that...

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.

However, when I use C headers in the < cX > style, I don't need to qualify the namespace. For example...

#include <cmath>
void f() {
  double var = sqrt( 17 );
}

This would compile fine. Even though the book says that using the < cX > header defines names in the std namespace only, you are allowed to use those names without qualifying the namespace. What am I missing here?

P.S. Using the GNU.GCC compiler

解决方案

Stephan T. Lavavej, a member of the MSVC team, addresses the reality of this situation (and some of the refinements to the standard) in this comment on one of his blog postings (http://blogs.msdn.com/vcblog/archive/2008/08/28/the-mallocator.aspx#8904359):

> also, <cstddef>, <cstdlib>, and std::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.)

I'm in 100% agreement with Lavavej, except I never tried to be very careful about using the <cfoo> style headers even when I first started using C++ - the standard C ones were just too ingrained - and there was never any real world problem using them (and apparently there was never any real world benefit to using the <cfoo> style headers).

这篇关于C ++库的范围 - &lt; X.h&gt; vs&lt; cX&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆