头文件顺序

Ben*_*min 3 c c++ windows dependencies header-files

可能重复:
C++标题顺序

// Here is module This.cpp
#include <boost/regex.hpp>
#include <iostream>
#include <math.h>
#include "mystring_written_by_c.h"
#include "mystring_written_by_cpp.h"
#include <list>
#include <string>
#include <stdio.h>
#include "stdafx.h" // for precomp
#include <tchar.h>
#include "This.h"
#include <Windows.h>
#include <Winsock2.h>
Run Code Online (Sandbox Code Playgroud)

它们现在按字母顺序排列.
我找到了订购它们的最佳做法.(有一个很好的配方吗?)

你将如何订购这些头文件?为什么?

Fre*_*urk 5

应尽可能地将标头写入1)独立于可能已包含的标题,以及2)不为后面包含的标题引入问题(例如公共标识符的宏).当这两个都为真时,您包含的顺序无关紧要.如果不是这样,您应该在自己的标题中修复它,或者在必要时处理它.

因此,选择是任意的,但您做出选择仍然很重要!"计划不算什么,但计划就是一切." 一致性导致更易读的代码.

一个相对常见的顺序是标准库,系统(如在OS中),外部库,然后是与当前文件相同的项目的标题 - 除了包含其"对应"标题的实现文件(如果有)之外的一个例外首先,在任何包含或其他代码之前.在每个组中,我按习惯按字母顺序排序,因为它再次完全是任意的,但是一些易于使用的顺序让我可以快速阅读和更新列表.

适用于您的清单:

// first only because this is required in your precompiled header setup
#include "stdafx.h"

// it's too bad this can't really be first
// I'm guessing "This" refers to the corresponding header
#include "This.h"

// C stdlib then C++ stdlib is what I usually do,
// whether the C headers are spelled XXX.h or cXXX.
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <list>
#include <string>

// someone mentioned winsock2 needs to be before windows
#include <Winsock2.h>

#include <tchar.h>
#include <Windows.h>

#include <boost/regex.hpp>

#include "mystring_written_by_c.h"
#include "mystring_written_by_cpp.h"
Run Code Online (Sandbox Code Playgroud)

上面划分为分组的线是故意的.我会留下评论为什么winsock2被赋予自己的组,但其余的评论通常不会出现.