主要内容

MISRA C++:2023 Rule 10.1.2

R2026b

The volatile qualifier shall be used appropriately

自 R2024b 起

描述

The volatile qualifier shall be used appropriately. 1

理由

避免对以下实体使用 volatile 限定:

  • 局部变量

  • 函数参数

  • 函数返回类型

  • 成员函数

  • 结构化绑定

如果对上述任何实体使用 volatile 限定,代码行为可能会变得难以理解且难以定义。

Polyspace 实现

如果使用 volatile 限定声明了以下任何实体,则 Polyspace® 会报告违反了此规则:

  • 函数返回类型

  • 局部变量

  • 函数参数

  • 结构化绑定

  • 类成员函数

故障排除

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

示例

全部展开

在此示例中,不当使用了 volatile 限定时,Polyspace 会报告违反了此规则。

#include <cstdint>

class MyClass {
	volatile void a_volatile_func() {} //Noncompliant - volatile class member function


};

volatile MyClass func1() { //Noncompliant - volatile return type
	//...
}

void func2() {
	volatile MyClass A ; //Noncompliant - volatile local variable
}

void func3(volatile MyClass A) { //Noncompliant - volatile func parameter
	int pair[2] = {1, 2};
	auto volatile [ a, b ] = pair;   //Noncompliant - volatile structured binding
}

检查信息

组:声明
类别:必需
PQL 名称:std.misra_cpp_2023.R10_1_2

版本历史记录

在 R2024b 中推出


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C:2025

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.