Which way this:
int main()
{
size_t a=0;
}
compiles with Visual Studio C++ 2022 (latest version) while it doesn’t compile with CLang while no headers included at all?
Of course, CLang is correct here. The question is not "Why?", the question is "In which way?".
Intellisense shows typedef unsigned long long size_t
, but both Go To Definition and Go To Declaration lead to error messages.
So, the question is, where is it defined and how comes to my source code?
(Precompiled headers turned off)
2
Answers
The question incorrectly assumes that
size_t
has to be defined somewhere in Visual C++. This is wrong. Some type names are built-in, and for Visual C++ that includessize_t
This is incorrect, because according to the standard the following code has to be legal:
The names of built-in types are reserved, and this code would have been illegal if it had used
float
instead ofsize_t
. But since no header is included,size_t
is not defined as a type, leaving the name available for the variable definition.The safe way is to go with std::size_t for C++ projects.
This approach is more robust and ensures that your code is not relying on implicit behavior, making it more likely to be portable across different compilers and platforms.