C++ 中 C 库的范围-

发布于 2024-08-18 15:20:14 字数 412 浏览 8 评论 0 原文

《C++ 编程语言:特别版》第 431 页指出...

对于每个标头 Xh>在全局命名空间和命名空间 std 中定义 C 标准库的一部分,有一个头文件 < CX>仅在 std 命名空间中定义相同的名称。

但是,当我在 < 中使用 C 标头时CX>样式,我不需要限定名称空间。例如...

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

这可以很好地编译。尽管书上说使用 < CX> header 仅定义 std 命名空间中的名称,您可以在不限定命名空间的情况下使用这些名称。我在这里缺少什么?

PS 使用GNU.GCC编译器

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一个人的旅程 2024-08-25 15:20:14

MSVC 团队成员 Stephan T. Lavavej 在他的一篇博客文章 (http://blogs.msdn.com/vcblog/archive/2008/08/28/the-mallocator .aspx#8904359):

>另外,还应该使用 std::size_t 等!

我以前对此非常小心。 C++98有一个美好的梦想,其中>将声明命名空间 std 中的所有内容,并且 将包含 ,然后使用 using 声明将所有内容拖到全局命名空间中。 (这是 D.5 [depr.c.headers]。)

许多实现者都忽略了这一点(其中一些实现者对 C 标准库头文件的控制很少)。因此,C++0x 已被更改以符合实际情况。截至 N2723 工作论文,http://open- std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf ,现在 保证声明名称空间 std 中的所有内容,并且可能会也可能不会在全局命名空间内声明事物。 则相反:它保证在全局命名空间内声明所有内容,并且可能会也可能不会在命名空间 std 内声明内容。

实际上,在 C++0x 中,包括 无论如何都无法防止在全局命名空间中声明的所有内容。这就是为什么我不再打扰

这是库第 456 期,http: //www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#456 .

(C++0x 仍然弃用 C 标准库中的 标头,这很搞笑。)

我 100% 同意 Lavavej,只是我从来没有试图变得非常即使当我第一次开始使用 C++ 时,也要小心使用 样式标头 - 标准 C 的标头太根深蒂固了 - 并且使用它们从来没有任何现实问题(而且显然从来没有使用 样式标头对现实世界有任何好处)。

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).

不交电费瞎发啥光 2024-08-25 15:20:14

C 库的规则与命名空间的 C++ 库不同

gcc 解释 Gcc 中的标准文档

该标准规定,如果包含 C 样式标头(在本例中为 ),则这些符号将在全局命名空间中可用,并且可能在命名空间 std:: 中可用(但这不再是一个严格要求。)另一方面,包含 C++ 样式标头 () 可保证在命名空间 std 中找到实体,并且可能在全局命名空间中找到实体。

在 C0X++ 规范草案中,它在第 17.6.2.3 标头部分中提到

未指定这些名称是否首先在全局命名空间范围内声明,然后注入
通过显式使用声明进入命名空间 std

The rule for the C libraries differs from C++ libraries for namespaces

gcc interprets the standard in Gcc docs as

The standard specifies that if one includes the C-style header (<math.h> in this case), the symbols will be available in the global namespace and perhaps in namespace std:: (but this is no longer a firm requirement.) One the other hand, including the C++-style header (<cmath>) guarantees that the entities will be found in namespace std and perhaps in the global namespace.

In the draft C0X++ spec it says in section 17.6.2.3 Headers

It is unspecified whether these names are first declared within the global namespace scope and are then injected
into namespace std by explicit using-declarations

护你周全 2024-08-25 15:20:14

如果不实现两次 C 库,就很难解决这个问题。请参阅 DR 456,基本上建议放弃这个问题。

It's hard to fix this without implementing the C library twice. See DR 456, which basically proposes giving up on the problem.

奈何桥上唱咆哮 2024-08-25 15:20:14

当它违反标准时,为什么你说“这会编译得很好”?谁允许您在不限定命名空间的情况下使用这些名称?您是否在特定实现上对此进行了测试并发现它有效?

我强烈建议不要使用某些特定的非标准功能,因为它恰好适用于您选择的编译器。这些东西很容易被破坏,也许使用同一编译器的更高版本。

Why do you say "This would compile fine" when it violates the Standard? Who allows you to use those names without qualifying the namespace? Have you tested this on a particular implementation and found that it works?

I strongly advise against using some particular non-standard feature because it happens to work on your compiler of choice. Such things break easily, perhaps with a later version of the same compiler.

游魂 2024-08-25 15:20:14

您可能缺少使用符合标准的编译器(或者您使用的编译器配置为与标准前的代码兼容)。

You are probably missing using a standards-conformant compiler (or the one you use is configured to be compatible with pre-standard code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文