#pragma once position: before or after # include's

In existing code, I saw that #pragma will be used after #include

//Some_Header.h
#include "header1.h"
#include "header2.h"

#pragma once

//implementations

Instead

//Some_Header.h
#pragma once

#include "header1.h"
#include "header2.h"

//implementations

I thought that you always need to be like the second example, does it matter when a single #pragma parameter is defined, or does the preprocessor select it anywhere in your file?

Edit

I know that #pragma once is not part of the standard and includes guards, but this is not my question.

+6
source share
1 answer

#pragma onceshould be placed before including any headers. The argument to the #pragma directive is subject to macro expansion. Thus, the contents of the included headers can change the behavior of the pragma:

// whatever.hpp
...
#define once lol_no

// your_header.hpp
#include "whatever.hpp"

#pragma once // warning C4068: unknown pragma
+8
source

Source: https://habr.com/ru/post/1016625/


All Articles