在现有代码中,我看到 #pragma once
在 header #include
s
//Some_Header.h
#include "header1.h"
#include "header2.h"
#pragma once
//implementations
代替
//Some_Header.h
#pragma once
#include "header1.h"
#include "header2.h"
//implementations
我认为它总是需要像第二个示例一样,您的 #pragma once
是在哪里定义的,还是预处理器在文件中的任何位置提取它是否重要?
编辑
我知道 #pragma once
不是标准的一部分,包括 guard 是,但这不是我的问题。
最佳答案
#pragma once
应该放在包含任何标题之前。 Argument of #pragma directive is a subject to macro expansion.因此包含的 header 的内容可以改变编译指示行为:
// whatever.hpp
...
#define once lol_no
// your_header.hpp
#include "whatever.hpp"
#pragma once // warning C4068: unknown pragma
关于c++ - #pragma 一次位置 : before or after #include's,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43492485/