skip to Main Content

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


  1. 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 includes size_t

    This is incorrect, because according to the standard the following code has to be legal:

    int main() {
      int size_t = 0;
      return size_t;
    }
    

    The names of built-in types are reserved, and this code would have been illegal if it had used float instead of size_t. But since no header is included, size_t is not defined as a type, leaving the name available for the variable definition.

    Login or Signup to reply.
  2. The safe way is to go with std::size_t for C++ projects.

    
    int main() {
        std::size_t a = 0;
    }
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search