首页 文章

#pragma一次位置:#include之前或之后[复制]

提问于
浏览
11

这个问题在这里已有答案:

在现有代码中,我看到#pragma曾在header #include之后使用过

//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曾经被定义的位置或者预处理器在文件中的任何地方拾取它是否重要?

Edit

我知道#pragma曾经不是标准的一部分,包括警卫,但这不是我的问题 .

1 回答

  • 12

    #pragma once 应放在包含任何 Headers 之前 . Argument of #pragma directive is a subject to macro expansion.因此,包含标头的内容可以改变编译指示行为:

    // whatever.hpp
    ...
    #define once lol_no
    
    // your_header.hpp
    #include "whatever.hpp"
    
    #pragma once // warning C4068: unknown pragma
    

相关问题