主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

AUTOSAR C++14 Rule A7-1-6

The typedef specifier shall not be used

描述

规则定义

The typedef specifier shall not be used.

理由

using 语法是定义别名时比 typedef-s 更优的选择。

自 C++11 开始,using 语法允许您定义模板别名,其中模板参量未绑定到数据类型。例如,以下语句为 vector 定义了一个别名 vectorType,其中参量 T 未绑定到数据类型,可稍后替换:

template<class T, class Allocator = allocator<T>> class vector;
template<class T> using vectorType = vector<T, My_allocator<T>>;
vectorType<int> primes = {2,3,5,7,11,13,17,19,23,29};
typedef 关键字不允许定义此类模板别名。

Polyspace 实现

规则检查项会标记所有使用 typedef 关键字的地方。

如果您不想删除某些 typedef 关键字的实例,请添加注释来申述这些结果。请参阅:

故障排除

如果您预期会出现违规,而 Polyspace® 未报告该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

#include <cstdint>
#include <type_traits>

typedef std::int32_t (*fptr1) (std::int32_t); //Noncompliant
using fptr2 = std::int32_t (*) (std::int32_t); //Compliant

template <class T> using fptr3 = std::int32_t (*) (T); //Compliant

fptr1fptr2 的别名定义完全等同。没有与 fptr3 别名定义对应的 typedef 等价项。

使用 typedef-s 违反了此规则。该规则要求您在任何情况下都必须使用 using 语法,即使存在 typedef 的等效形式。

检查信息

组:声明
类别:必需、自动

版本历史记录

在 R2019a 中推出