警告如果一个类型的另一个typedef定义名称参数列表使用 [英] Warn if another typedef'd name of a type is used in an argument list

查看:156
本文介绍了警告如果一个类型的另一个typedef定义名称参数列表使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个大的项目,其中许多类型的typedef 'D,例如

Consider a large project, where many types are typedef'd, e.g.

typedef int age;
typedef int height;

和某些功能得到这些类型的参数:

and some functions getting arguments of those types:

void printPerson(age a, height h) {
    printf("Age %d, Height %d\n", a, h);
}

有没有办法在编译时警告,如果这些参数是错误的类型,例如

Is there a way to warn at compile time, if those arguments are of the wrong type, e.g.

age a = 30;
height h = 180;
printPerson(h, a); /* No warning, because a and h are both integers */

海湾合作委员会(或一些静态code分析工具)是否有一个选项,在这种情况下,警告?

Does gcc (or some static code analysis tool) have an option to warn in such cases?

推荐答案

有没有内置在海湾合作委员会的支持。

There is no built-in support for this in GCC.

功能要求添加此基础上,稀疏 nocast 属性。然而,这并没有得到落实。如果您可以使用稀疏,不过,你可以通过标记与 __ __属性((nocast))

There is a feature request to add this, based on the Sparse nocast attribute. However, this hasn't been implemented. If you can use Sparse, though, you could do this by marking each typedef with __attribute__((nocast)).

在C ++中你可以通过包装类,而不是类型定义做到这一点,然后根本不定义它们隐式转换。

In C++ you can do this by making wrapper classes rather than typedefs, and then simply not defining implicit conversions for them.

这篇关于警告如果一个类型的另一个typedef定义名称参数列表使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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