为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作



还有比在哪里找到size_t的定义更多的问题?'s的答案。

#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;
}

在Visual Studio 2019中:

size_t的定义似乎在vcruntime.h 中

(1( size_t即使不包含头也能工作,为什么?构建系统是否可以自动包含像vcruntime.h这样的头,或者这是由其他机制实现的,比如内置类型?

(2( std::size_t只能在包含iostream时使用,F12也可以跳转到vcruntime.h中的定义。但是搜索整个MS VC运行时来源(例如…\VC\Tools\MSVC\14.23.28105(,似乎没有这样的定义,那么这是如何发生的?

现在我发现以上情况也发生在Windows上的Clang 9上。

在您的示例中,size_t是从cstdlib中找到的。

由于定义了c++17 size_t:

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

在vs或其他编译器中,它可以在没有头的情况下工作。有时,它可以通过其他基本的头文件(如i/o(找到,甚至可以通过使用std找到。但最好包含一个头文件。

最新更新