Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 945
Trevor Hickey Avatar asked May 04 '12 08:05

Trevor Hickey


People also ask

Do you have to include cstdlib?

iostream may include cstdlib directly or indirectly. This brings std::rand() and ::rand() in the scope. You are using the latter one. But yes, you should not count on this and always include cstdlib if you want to use rand .

What does #include cstdlib mean?

What Is cstdlib in C++? The C++ Standard Library header file (cstdlib in C++) is the header for one of the most widely used libraries by programmers of the language. This header defines a collection of functions and macros to facilitate efficient, high-performing, standardized C++ code across teams and platforms.

Does iostream include Stdlib?

After the include of iostream, it seems that stdlib. h is also included automatically because when I write "merg",the Code Completion of Xcode gives me the function "mergesort" which is a function in the stdlib. h according to the documentation of Xcode.


1 Answers

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)

like image 139
Fred Foo Avatar answered Sep 23 '22 22:09

Fred Foo