简体   繁体   English

c ++ 11包括 <cstdlib> 有时c ++ 03不会?

[英]c++11 includes <cstdlib> at times when c++03 will not?

Take a look at this tiny program. 看看这个小程序吧。

#include <iostream>

int main(){

  int var = atoi("-99");      //convert string to int
  var = abs(var);             //takes absolute value
  std::cout << var+1 <<'\n';  //outputs 100

  return EXIT_SUCCESS;
}

Compiling creates the following errors messages: 编译会创建以下错误消息:

$ g++ -o main main.cpp
main.cpp: In function ‘int main()’:
main.cpp:5:13: error: ‘atoi’ was not declared in this scope
main.cpp:6:16: error: ‘abs’ was not declared in this scope
main.cpp:9:10: error: ‘EXIT_SUCCESS’ was not declared in this scope

Understandable. 可以理解的。 All of these exist in the "cstdlib" header which I neglected to include. 所有这些都存在于我忽略的“cstdlib”标题中。
However, compiling with: 但是,编译:

$ g++ -std=c++0x -o main main.cpp 

creates no issues. 没有问题。


looking at the source of the "cstdlib" header, I see the following code at the bottom: 查看“cstdlib”标头的来源,我在底部看到以下代码:

#ifdef __GXX_EXPERIMENTAL_CXX0X__
#  if defined(_GLIBCXX_INCLUDE_AS_TR1)
#    error C++0x header cannot be included from TR1 header
#  endif
#  if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
#    include <tr1_impl/cstdlib>
#  else
#    define _GLIBCXX_INCLUDE_AS_CXX0X
#    define _GLIBCXX_BEGIN_NAMESPACE_TR1
#    define _GLIBCXX_END_NAMESPACE_TR1
#    define _GLIBCXX_TR1
#    include <tr1_impl/cstdlib>
#    undef _GLIBCXX_TR1
#    undef _GLIBCXX_END_NAMESPACE_TR1
#    undef _GLIBCXX_BEGIN_NAMESPACE_TR1
#    undef _GLIBCXX_INCLUDE_AS_CXX0X
#  endif
#endif

I'm not sure if that is relevant or not.. full header file code here 我不确定这是否相关.. 这里是完整的头文件代码

my ultimate question is, does the new standard guarantee that all of cstdlib will be brought in at a global namespace when you include iostream? 我的最终问题是,当你包含iostream时,新标准是否保证所有cstdlib都会被引入全局命名空间?

I can't find any documentation on the matter. 我找不到有关此事的任何文件。 Appears that way to me, does it appear that way to you? 对我来说就是这样,对你来说是这样吗?

gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

my ultimate question is, does the new standard guarantee that all of cstdlib will be brought in at a global namespace when you include iostream? 我的最终问题是,当你包含iostream时,新标准是否保证所有cstdlib都会被引入全局命名空间?

No. You should #include it yourself if you need its functionality. 不,如果您需要它的功能,您应该自己#include它。 If you get it "for free" with <iostream> , that's a sign that your <iostream> header requires it, but then you're relying on an implementation detail of your C++ library. 如果你使用<iostream> “免费”获得它,那表明你的<iostream>标头需要它,但是你依赖于你的C ++库的实现细节。

Btw., #include <cstdlib> is not guaranteed to bring C functions into the global namespace (although it commonly does so in C++ implementations); 顺便说一下, #include <cstdlib>不能保证将C函数带入全局命名空间(尽管在C ++实现中通常会这样做); it is guaranteed to put them in the namespace std : 保证将它们放在命名空间std

Except as noted in Clauses 18 through 30 and Annex D, the contents of each header cname shall be the same as that of the corresponding header name.h , as specified in the C standard library (1.2) or the C Unicode TR, as appropriate, as if by inclusion. 除第18条至第30条和附件D中所述外,每个标题cname的内容应与C标准库(1.2)或C Unicode TR中指定的相应标题name.h的内容相同。 ,好像通过包含。 In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std . 但是,在C ++标准库中,声明(除了在C中定义为宏的名称除外)都在命名空间std的命名空间范围(3.3.6)内。 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 (7.3.3). 未指定这些名称是否首先在全局命名空间范围内声明,然后通过显式using -declarations(7.3.3)注入命名空间std

(Standard, section 17.6.1.2) (标准,第17.6.1.2节)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM