#pragma once 位置:#include 之前或之后
#pragma once position: before or after #include's
在现有代码中,我看到 #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
不是标准的一部分,包括守卫也是,但这不是我的问题。
#pragma once
应该放在 headers 之前。 Argument of #pragma directive is a subject to macro expansion. 所以包含的内容 headers 可以改变 pragma 行为:
// whatever.hpp
...
#define once lol_no
// your_header.hpp
#include "whatever.hpp"
#pragma once // warning C4068: unknown pragma
在现有代码中,我看到 #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
不是标准的一部分,包括守卫也是,但这不是我的问题。
#pragma once
应该放在 headers 之前。 Argument of #pragma directive is a subject to macro expansion. 所以包含的内容 headers 可以改变 pragma 行为:
// whatever.hpp
...
#define once lol_no
// your_header.hpp
#include "whatever.hpp"
#pragma once // warning C4068: unknown pragma