c++ - pragma 指令的范围是什么?

标签 c++ visual-studio-2008 scope header-files pragma

pragma 指令的范围是什么?例如,如果我在另一个文件 B 中包含的头文件 A 中说 #pragma warning(disable: 4996),那是否也会禁用 B 中的所有警告?还是应该再次启用文件 A 末尾的警告?

最佳答案

直到翻译单元结束。通俗地说,TU 是源文件及其包含文件。

通常的模式是这样的:

#pragma warning (push) //save
#pragma warning (disable: xxxx)
#pragma warning (disable: yyyy)
...

//code

#pragma warning (pop) //restore prev settings

例如

//A.h
#pragma once
#pragma warning (disable: 1234)
#include "b.h"

//b.h
#pragma once
//when included after a.h 1234 will be disabled

//c.cpp
#include "a.h" //warnings 1234 from b.h is disabled

//d.cpp
#include "b.h" //warnings 1234 from b.h are not disabled
#include "a.h"

关于c++ - pragma 指令的范围是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5078679/

相关文章:

asp.net - 将 Visual Studio 本地 IIS 7 调试虚拟路径设置为/

c - C 中的堆栈溢出错误,在任何步骤之前

c++ - STL 容器的奇怪行为(构造/销毁和范围)

javascript - 如何使 JS 对象在多个文件中可用

scope - 如何编写不使用其参数隐藏变量的 PicoLisp 函数

c++ - 在编译时静态检查 map ?

c++ - CScrollBar 拇指跟踪不起作用

c++ - 当基于 CDialog 的应用程序启动时,如何将我的辅助对话窗口置于顶部?

c++ - 在带有 Eclipse 的 Linux 中是否有 wxwidgets 的图形用户界面设计器?

c++ - 我如何链接到 OpenGL?