Documentation states that size_t is defined in header <cstddef>. However, the following program runs using size_t even though I've commented out the include. My question is: how can this program compile and run with size_t as a type if the header that defines size_t is not included? The program declares two built-in arrays, fills the first array's elements with values equal to its index, displays them, then copies the values to a second array and displays them:
To my knowledge, there's no rule to prevent library headers from including each other.
In your case, the iostream header directly or indirectly includes the cstddef header, or whatever "internal" header the library implementers wrote to define std::size_t in.
So this code compiles for you, but remember that it is not portable: other people using other compilers may get an error because cstddef was not included. Always explicitly include the headers from which you use something.
To my knowledge, there's no rule to prevent library headers from including each other.
In your case, the iostream header directly or indirectly includes the cstddef header, or whatever "internal" header the library implementers wrote to define std::size_t in.
I'm using Visual Studio 2013, and this program was created as an empty project. Before posting the question I checked the includes of <iostream>, and found that the result of including <iostream> causes a chain of <istream>, <ostream>, and <ios> includes, but not <cstddef> as far as I can tell. So there must be some 'internal' hidden includes as you say (or maybe microsoft's own version of a global size_t?). Yes, I agree with your comment about the portability.
edit: following up on the 'global size_t' idea, notice that I didn't have to declare size_t as being in the std namespace...just a plain size_t instead of a std::size_t
hmm...
iostream
-> istream
-> ostream
-> ios
-> xlocnum (uses size_t)
-> cstdio
-> stdio.h (a-ha! this would explain the global size_t)
-> crtdefs.h (the actual typedefs of size_t are located here)
Documentation states that size_t is defined in header <cstddef>.
Yes. It also states in the C standard that size_t is defined in multiple headers (other than <stddef.h>) Two of those are stdlib.h and stdio.h, which, in the case of VC++, are included indirectly by <iostream>.
Regarding the size_t type in my program...it's not the one from cstddef...it's microsoft's version, as stated in this page:
So xlocnum gets size_t into it! I checked the includes as far as <ios> and said...nah xlocnum won't get it...silly me! So there's no difference between size_t and std::size_t?