简体   繁体   中英

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

#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?

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?

No. You should #include it yourself if you need its functionality. 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.

Btw., #include <cstdlib> is not guaranteed to bring C functions into the global namespace (although it commonly does so in C++ implementations); it is guaranteed to put them in the namespace 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. 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 . 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).

(Standard, section 17.6.1.2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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