为什么CERT标准pre00-CPP说:"避免定义宏" [英] Why CERT standard PRE00-CPP says "Avoid defining macros"

查看:144
本文介绍了为什么CERT标准pre00-CPP说:"避免定义宏"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据<一href=\"https://www.securecoding.cert.org/confluence/display/cplusplus/$p$p00-CPP.+Avoid+defining+macros\"相对=nofollow> CERT标准 对象宏可能是危险的,因为其用途不受语言的范围规则。任何一个可以请用一个例子来它是如何解释问题。例如说,我有一个对象,类似宏的#define BUFFER_SIZE 1024


解决方案

  //文件a.hpp
#定义SOME_MACRO 42//一些文件/嵌套/ DIR /真的/远/中/ x.hpp
#定义SOME_MACRO 13

KABOOM。这将很容易地被使用任何种类的范围,例如可以避免的。

 结构X {静态无符号常量的值= 42; };
Ÿ结构静{无符号常量的值= 13; };

现在,您可以访问 X ::值Ÿ::值。同去的命名空间:

 命名空间的结构{X; }
命名空间B {结构X; }

According to the CERT standards Object-like macros can be dangerous because their uses are not subject to the language scope rules. Can any one please explain with an example How it is problematic. say for example I have an Object-like macro #define BUFFER_SIZE 1024

解决方案

// file a.hpp
#define SOME_MACRO 42

// file some/nested/dir/really/far/in/x.hpp
#define SOME_MACRO 13

Kaboom. This would have easily been avoided using any kind of scope, e.g.

struct X { static unsigned const value = 42; };
struct Y { static unsigned const value = 13; };

Now you can access X::value and Y::value. Same goes for namespaces:

namespace a { struct X; }
namespace b { struct X; }

这篇关于为什么CERT标准pre00-CPP说:&QUOT;避免定义宏&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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