上述&lt声明C函数;ç____>头保证在全局命名空间以及性病? [英] are C functions declared in <c____> headers guaranteed to be in the global namespace as well as std?

查看:112
本文介绍了上述&lt声明C函数;ç____>头保证在全局命名空间以及性病?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这一点是我一直想知道,但从来没有相当肯定。因此,它是严格好奇的问题,而不是一个真正的问题。

So this is something that I've always wondered but was never quite sure about. So it is strictly a matter of curiosity, not a real problem.

据我了解,whenyou做类似的#include< cstdlib> 一切(当然,除了宏)中声明的 STD :: 命名空间。我见过的每一个执行情况做类似下面这样处理:

As far as I understand, whenyou do something like #include <cstdlib> everything (except macros of course) are declared in the std:: namespace. Every implementation that I've ever seen does this by doing something like the following:

#include <stdlib.h>
namespace std {
    using ::abort;
    // etc....
}

这当然有东西两个全局命名空间是效果和 STD 。这种行为是保证?或者是有可能的实现可能把这些东西在 STD ,但不是在全局命名空间?我能想到的唯一办法做到这一点是有你的libstdc ++实现每一个C函数本身将它们放置在 STD 直接,而不是只包括现有的libc头(因为有没有机制来移除命名空间的东西)。这当然是一个很大的努力几乎没有任何好处。

Which of course has the effect of things being in both the global namespace and std. Is this behavior guaranteed? Or is it possible that an implementation could put these things in std but not in the global namespace? The only way I can think of to do that would be to have your libstdc++ implement every c function itself placing them in std directly instead of just including the existing libc headers (because there is no mechanism to remove something from a namespace). Which is of course a lot of effort with little to no benefit.

我的问题的实质是,就是下面的程序严格遵循规则,并保证工作?

The essence of my question is, is the following program strictly conforming and guaranteed to work?

#include <cstdio>
int main() {
    ::printf("hello world\n");
}

编辑:我发现的最接近的是这个(17.4.1.2p4):

The closest I've found is this (17.4.1.2p4):

除了通过如第18注意到
  27,每个报头CNAME的内容
  应是相同的
  相应的头name.h,如
  在ISO / IEC规定的9899:1990
  编程语言C(第7条),或
  ISO / IEC:1990编程语言-C
  修订1:C完整性,(第7条),
  适当的,仿佛被列入。在
  在C ++标准库,但是,
  该声明和定义
  (除了名称其被定义为
  在C宏)的命名空间中
  命名空间std范围(3.3.5)。

Except as noted in clauses 18 through 27, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in ISO/IEC 9899:1990 Programming Languages C (Clause 7), or ISO/IEC:1990 Programming Languages—C AMENDMENT 1: C Integrity, (Clause 7), as appropriate, as if by inclusion. In the C + + Standard Library, however, the declarations and definitions (except for names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std.

这是说实话,我可以跨preT两种方式。 每头CNAME的内容应当相同相应的头name.h,如在ISO指定/ IEC 9899:1990编程语言C告诉我,他们可能会在全局命名空间是必需的,但在C ++标准库,但是,声明和定义(除名称
它被定义为C宏)是空间std的命名空间范围(3.3.5)之内。说,他们是在STD(但不指定任何其他范围的他们是)。

which to be honest I could interpret either way. "the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in ISO/IEC 9899:1990 Programming Languages C" tells me that they may be required in the global namespace, but "In the C + + Standard Library, however, the declarations and definitions (except for names which are defined as macros in C) are within namespace scope (3.3.5) of the namespace std." says they are in std (but doesn't specify any other scoped they are in).

推荐答案

下面的情况从MSVC队的斯蒂芬T. Lavavej(HTTP一个不错的概要(有一些relaity主场迎战标准说什么)://博客.msdn.com / vcblog /存档/ 2008/08/28 /最mallocator.aspx#8904359):

Here's a nice synopsis of the situation (with some relaity vs. what the standard says) from Stephan T. Lavavej of the MSVC team (http://blogs.msdn.com/vcblog/archive/2008/08/28/the-mallocator.aspx#8904359):

&GT; 此外,&LT; cstddef&GT; &LT; cstdlib&GT; 的std ::为size_t 等应使用!

> also, <cstddef>, <cstdlib>, and std::size_t etc should be used!

我曾经是非常小心的。 C ++ 98有一个灿烂的梦想,其中&LT; cfoo&GT; 将宣布空间std内的所有内容,而&LT; foo.h中&GT; 将包括&LT; cfoo&GT; 键,然后将一切都变成全局命名空间使用-声明。 (这是D.5 [depr.c.headers]。)

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

这是由大量的实施者(其中一些已经在C标准库头很难控制)忽略。因此,C ++ 0x中已更改为与现实相符。由于N2723工作文件,<一的href=\"http://open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf\">http://open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2723.pdf现在&LT; cfoo&GT; 是保证申报空间std内的所有内容,并可能会或可能不会全局命名空间内声明的东西。 &LT; foo.h中&GT; 则相反:它是保证全局命名空间内声明的一切,可能会或可能不会空间std中声明的东西。

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.

在现实与C ++ 0x中,包括&LT; cfoo&GT; 反对一切都没有保障在全局命名空间得到声明反正。这就是为什么我用停止打扰&LT; cfoo方式&gt;

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

这是图书馆456期,<一个href=\"http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#456\">http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#456

This was Library Issue 456, http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#456 .

(C ++ 0x中仍德precates从C标准库,这是热闹的标头。)

(C++0x still deprecates the headers from the C Standard Library, which is hilarious.)

我从来没有喜欢的&LT的; cfoo&GT; 头自己,发现我一直使用&LT; foo.h中&GT ; 。现在,我觉得我可以不要再担心我缺乏C ++在这方面,'纯洁'的。

I've never been fond of the <cfoo> headers myself, and found that I've always use <foo.h>. Now I feel like I can stop being anxious about my lack of C++ 'purity' in that regard.

这篇关于上述&lt声明C函数;ç____&GT;头保证在全局命名空间以及性病?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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