在同一个头文件中包含一次防护和#pragma [英] Include guard and #pragma once in the same header file
问题描述
引用 Microsoft文档,There is no advantage to use of both the #include guard idiom and #pragma once in the same file.
先前有关stackoverflow的相关问题的答案也证实,同时拥有这两者毫无意义.例如,请参见下文:
Answers to previous related questions on stackoverflow also confirm that it is pointless to have both. See below, for instance:
boost
库的vector.hpp
文件由此开始:
#ifndef BOOST_ASSIGN_STD_VECTOR_HPP
#define BOOST_ASSIGN_STD_VECTOR_HPP
#if defined(_MSC_VER)
# pragma once
#endif
...
#endif
也就是说,它既包括后卫习语,也包括一次实用语. boost头文件同时具有这两个原因吗?
That is, it includes both the guard idiom as well as the pragma once. Is there any reason why boost header files have both?
推荐答案
技术上#pragma once
不是标准的C ++,而标头保护符则是.如果两者都有,它们不会互相冲突.
Technically #pragma once
is not standard C++, whereas header guards are. They will not conflict with each other if you have both.
#if defined(_MSC_VER)
暗示,boost可能兼有的原因是,如果您不使用MSVC,则需要某物作为标头后卫,因此它们会退回到另一种方法.
The reason boost likely has both, as alluded to by the #if defined(_MSC_VER)
is that if you're not using MSVC then you need something to act as your header guard, so they fall back to the other method.
由于boost致力于跨平台,因此他们试图确保其代码在不支持#pragma once
的编译器上运行,尽管我能想到的所有大型现代编译器都支持它,例如在维基百科上进行枚举.
Since boost strives to be cross-platform they are trying to ensure their code works on compilers that don't support #pragma once
, though all of the big modern compilers I can think of do support it, as enumerated on wikipedia.
这篇关于在同一个头文件中包含一次防护和#pragma的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!