简体   繁体   English

C ++标头本身不包含依赖标头

[英]c++ header does not include dependent header on its own

Is there a reason why tlhelp32.h does not include windows.h itself ? tlhelp32.h不包含windows.h本身是否有原因? I was fighting with tons of compiler errors because I included windows.h after including tlhelp32.h. 我要处理大量的编译器错误,因为在包含tlhelp32.h之后包含了windows.h。 Is it a design decision or for what reason ? 是设计决定还是出于什么原因? I am new to c++ so i dont get it. 我是C ++的新手,所以我不明白。 If a header has dependencies it should include them. 如果标头具有依赖项,则应包括它们。

#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>

std::vector<unsigned long> GetProcessIdsHelper()
{
std::vector<ULONG> result;
auto snapshotHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (snapshotHandle == INVALID_HANDLE_VALUE)
    return result;

PROCESSENTRY32 processEntry;
if (!Process32First(snapshotHandle, &processEntry))
    return result;

do
    result.push_back(processEntry.th32ProcessID);
while (Process32Next(snapshotHandle, &processEntry));

return result;
}

Most of the other Windows headers don't #include <windows.h> , but take for granted that you've done so. 其他大多数Windows标头都不包含#include <windows.h> ,但是您已经这样做了。

As far as I can tell, the "why" is that they basically assume the first line of every file other than Windows.h (and the headers it includes) is #include <windows.h> , so checking for it would be a waste of time. 据我所知,“为什么”是他们基本上假设除Windows.h以外的每个文件的第一行(及其包含的头文件)都是#include <windows.h> ,因此检查它是浪费时间。

This basically comes down to a collision of two different styles. 这基本上归结为两种不同样式的冲突。 What I'd call the "traditional" C view was that each header just defined its own entities. 我称之为“传统的” C观点是,每头定义了自己的实体。 If it used entities from other files, the person writing the source was expected to know all the headers, and the order in which they needed to be included. 如果它使用其他文件中的实体,则编写源代码的人员应该知道所有标头以及需要包含它们的顺序。 This kept life simple for library authors , at the expense of (frequently) making the libraries they produced more difficult for users to actually use. 这使图书馆作者的生活变得简单,但要付出(经常)使他们制作的图书馆更难为用户实际使用的代价。

When the C committee started to standardize C, they more or less rejected this approach, opting instead for an approach where each header looked independent from a user's point of view, and (in nearly all cases) inclusion of a header was idempotent (ie, repeatedly including the same header, directly or indirectly, didn't cause a problem). 当C委员会开始对C进行标准化时,他们或多或少都拒绝了这种方法,而是选择了一种方法,即每个标头看上去都与用户的观点无关,并且(几乎在所有情况下)标头的加入是幂等的(即,重复地直接或间接包含相同的标头不会造成问题)。 This generally makes life simpler for the person using a library, at the expense of making the library author's life a little more difficult. 通常,这会使使用图书馆的人的生活更简单,但以使图书馆作者的生活更加困难为代价。

Windows.h itself sort of leans toward the latter, but many of the other headers that depend on it lean more toward the former (at least in the specific case of windows.h). Windows.h本身倾向于后者,但是依赖于它的许多其他头文件则更倾向于后者(至少在windows.h的特定情况下)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM