为什么 C++ Windows Store app header 文件对包含在 cpp 文件中的内容感到满意?

Why are C++ Windows Store app header files satisfied with includes in cpp files?

最近写了一个 Windows Store 应用程序,我很惊讶我可以使用 std::string 类型而不必包含它的 header。更具体地说,以下代码编译没有任何错误:

StringExample.h:

#pragma once

class StringExample
{
public:
    std::string str;
};

StringExample.cpp:

#include "pch.h"

#include "StringExample.h"

#include <string>

从 .cpp 文件中删除包含的字符串会使编译器失败。 header 文件中不应该已经需要字符串 header 吗?

我的错。我仔细检查了一个空项目中包含的顺序,.cpp 文件确实需要看起来像这样才能发生:

#include "pch.h"

#include <string>

#include "StringExample.h"

因此,包含 <string> 允许 StringExample.h 编译,而删除包含会使编译失败。