1

Some more question than Where do I find the definition of size_t? 's answers.

#include <iostream> //comment this size_t still works, but std::size_t doesn't work
#include <stdlib.h> //comment this size_t still works

int main(int argc, char* argv[])
{        
    size_t t;
    std::size_t t;
    return 0;
}

In Visual Studio 2019:

The definition of size_t seems to be in vcruntime.h

(1) size_t works even no header is included, why? Can the build system auto include some header like vcruntime.h, or is this implemented by some other mechanism like built in types?

(2) std::size_t can be used only when iostream is included, and F12 jump to the definition in vcruntime.h too. But searching the whole MS VC runtime source (e.g. ...\VC\Tools\MSVC\14.23.28105), there seems to be no such definition, then how does this happen?

Now I found that the above happen with Clang 9 on Windows too.

jw_
  • 1,663
  • 18
  • 32
  • "*std::size_t can be used only when iostream is included*" Only? It's found in `` and/or ``. – Nicol Bolas Feb 24 '20 at 04:26
  • I mean, why does `int` compiles when there is no header included? Well, there is a bunch of default mistery includes whenever you want to build something. – ALX23z Feb 24 '20 at 04:28
  • @ALX23z Now I get it, since size_t is typedef of uint/ulong, not a struct, then it is normal that some mistery happens to it. – jw_ Feb 24 '20 at 04:38
  • 1
    @jw_: No, that makes no sense. `size_t` is a typedef and therefore must have been typedef'd *before* it can be use. You cannot use a typedef before it actually gets typedef'd. `int` compiles because it's a keyword defined by the standard and is provided directly by the implementation. – Nicol Bolas Feb 24 '20 at 04:42
  • @NicolBolas Now I searched and/or there is only "using _CSTD size_t;" under std{} which means "using ::std::size_t;" - will "using" become a definition? I think not. – jw_ Feb 24 '20 at 04:46
  • @jw_: You can search whatever you like, but the C++ standard requires that including one of those headers will mean that `std::size_t` will be available to you. How the implementation goes about making that happen is kind of irrelevant. – Nicol Bolas Feb 24 '20 at 04:47
  • @NicolBolas Yes that seems to be a bad comparison from ALX23z, but think it again I get that the meaning may be int/long is treat specially, then a typedef of int/long is not shocking to be treated specially by the compiler too. It is highly possible that the compiler treat size_t specially. – jw_ Feb 24 '20 at 04:48
  • @NicolBolas Mostly irrelevant, but this is what the question asked for. I'm using Clang libtooling so this is relevant to me. – jw_ Feb 24 '20 at 04:50
  • @jw_: "*It is highly possible that the compiler treat size_t specially.*" If it does, that would be an implementation detail which is not reliable across compilers. And therefore, there's no point in trying to rely on being able to use `size_t` without including an appropriate header. And if you shouldn't rely on it... what does it matter how it works? – Nicol Bolas Feb 24 '20 at 04:55
  • 1
    [compiler explorer example with msvc, clang, gcc](https://godbolt.org/z/HvajpF) only msvc compiles when there's no #includes. I'm surprised by this but it's totally acceptable afaict. – Ryan Haining Feb 24 '20 at 04:58
  • @NicolBolas My concern is not the usage C++, but the type look up process during build. – jw_ Feb 24 '20 at 05:28
  • @RyanHaining Clang 9 Windows doesn't report error too. - This may mean (1) Clang emulate MSVC's behavior on Windows (2) The reason is from some windows specific header files auto included instead of built in types - will the build system do that? – jw_ Feb 24 '20 at 05:32
  • So the answer seems to be that it is built-in typedef in msvc and clang-cl emulates that. Is this allowed by the standard on the other hand? I guess it's hard to say, maybe it's not explicitly forbidden and thus it's fine. MSVC devs could remove it in `/permissive-` mode probably to be more compatible, but I'm not sure that they will. – Predelnik Jun 02 '21 at 11:27

1 Answers1

0

From your example size_t is found from cstdlib.

Since c++ 17 size_t is defined :

in header <cstddef>
in header <cstdio>
in header <cstdlib>
in header <cstring>
in header <ctime>
in header <cuchar>

it may work without header in vs or other compilers. sometimes it can be found through other basic headers like i/o or even by the using std. But it's safe/better to include a header.

sohel14_cse_ju
  • 2,481
  • 9
  • 34
  • 55