☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

不应使用宏定义类型

3.2.9 ID_macro_typeid
目录 › next › previous

宏用于文本处理,不受语言规则限制,不应使用宏实现类型等语言层面的概念。

示例:

#define ptr_type int*   // Non-compliant
ptr_type p0, p1;        // p0 is a pointer, p1 is an integer

例中宏展开后相当于 int* p0, p1,p0 是指针,但 p1 却不是指针,极易引起误解。

应使用 typedef 等方法定义类型:

typedef int* ptr_type;  // Compliant
ptr_type p0, p1;        // p0 and p1 are all pointers

相关

ID_macro_sideEffectArgs ID_macro_const ID_macro_function

参考

C++ Core Guidelines ES.30
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.