编译指示的范围是什么? [英] What is the scope of a pragma directive?

查看:68
本文介绍了编译指示的范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the scope of a pragma directive? For example, if I say #pragma warning(disable: 4996) in a header file A that is included from a different file B, will that also disable all those warnings inside B? Or should I enable the warning at the end of file A again?

推荐答案

直到翻译单元结束.非正式地,TU是包含其包含文件的源文件.

It is till the end of the translation unit. Informally, a TU is the source file with its include files.

通常的模式是这样的:

#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"

这篇关于编译指示的范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆