确保使用自定义类型
Ensure use of custom types
考虑到typedefs
对基本类型的好处以及为什么使用它们,有什么方法可以确保在您的项目中没有使用基本类型,而是使用typedef对应的类型吗?
如果真的、绝对地想要禁止本机类型但允许typedefs
,我想你总是可以这样做:
#include <stdint.h>
#define int please_use_stdint_typedefs_rather_than_native_types
int main()
{
int32_t good; // Good typedef.
int evil; // Evil native type.
}
$ gcc -c int_forbidden.c
int_forbidden.c: In function ‘main’:
int_forbidden.c:8: error: ‘please_use_stdint_typedefs_rather_than_native_types’ undeclared (first use in this function)
int_forbidden.c:8: error: (Each undeclared identifier is reported only once
int_forbidden.c:8: error: for each function it appears in.)
int_forbidden.c:8: error: expected ‘;’ before ‘evil’
也就是说,我认为在一般情况下,彻底禁止原生类型不是一个好主意。
您可以按照这个boost库中的建议,使这些类型定义成为强类型定义:http://www.boost.org/doc/libs/1_40_0/boost/strong_typedef.hpp
考虑到typedef只是一个类型的同义词,实际上并没有创建一个新类型,我认为没有任何可靠的方法来确保这一点。您可以编写一个脚本来运行代码,并查找基元类型与预期的typedef对应类型的出现情况。
相关文章:
- 使用QJsEngine在Qt中注册自定义类型
- 使用模板类的自定义列表类型中的内存泄漏
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 如何使自定义类型在unordered_map中用作键
- 自定义引用类型
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 如何定义自定义生成配置类型,其中通常是.exe的目标改为 DLL
- 标准::原子中的自定义类型
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- 在Qt中注册自定义元类型的别名类型
- 如何在C++中允许成员函数的自定义返回类型进行类型擦除?
- 如何使用自定义类型声明Arduino数组?
- 特征中的自定义标量类型
- 在点云库 (PCL) 中使用自定义点类型的问题
- 为自定义打印调试实现传递任何类型的变量
- 使用自定义访问者时具有自定义类型的提升变体失败(源自 boost::static_visitor)
- 错误 - 自定义数据类型作为有效负载,带有提升::几何
- 将成员函数的返回类型引用到C++中的自定义类