1

I've noticed that my C++ programs compile fine whether I use ::size_t or std::size_t. I can use them interchangeably with no issues at all, so it seems like one of them is a typedef for the other.

As an example, consider the following code which uses the global size_t (this is the whole file, no usings and other stuff):

#include <iostream>
int main() {
    ::size_t x = 100;
    std::cout << x << std::endl;
}

The next code uses the size_t in std:

#include <iostream>
int main() {
    std::size_t x = 100;
    std::cout << x << std::endl;
}

Both compile fine and outputs 100 as expected.

I was under the impression that everything in the standard library is put in namespace std, but clearly this isn't the case. Why is this so?

Note: the same goes for ptrdiff_t, intN_t and uintN_t too.

Bernard
  • 5,209
  • 1
  • 34
  • 64
  • Maybe dupes, if not interesting reads: https://stackoverflow.com/questions/237370/does-stdsize-t-make-sense-in-c and https://stackoverflow.com/questions/42797279/what-to-do-with-size-t-vs-stdsize-t – Tas May 31 '17 at 04:06
  • @Tas Now I have another question. If I remove `#include ` and add `#include `, `::size_t` will not be defined. So it seems `#include ` internally does `#include `, at least on GCC. – Bernard May 31 '17 at 04:16
  • @Bernard, the C++ standard does not place a restriction on the additional headers included by any standard library header. So, that behavior of GCC isn't out of line with the standard – WhiZTiM May 31 '17 at 04:31
  • The duplicate is related but it doesn't seem to really answer this question – M.M May 31 '17 at 04:36
  • @WhiZTiM That comes as a surprise to me. Many people say that as long as you don't do `using namespace std;` you'll be free from clashes with names in the standard library. – Bernard May 31 '17 at 08:18

1 Answers1

-1

According to what I've understood,::size_t and std::size_t are slightly different, but essentially the same, with a similar function.

There's a much better answer here: link

Hopes this helps!

Omer Hen
  • 13
  • 6