このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。
MISRA C++:2008 Rule 7-2-1
An expression with enum underlying type shall only have values corresponding to the enumerators of the enumeration.
R2023a 以降
説明
ルール定義
An expression with enum underlying type shall only have values corresponding to the enumerators of the enumeration. 1
根拠
enum
潜在型を含む式をプログラムが評価し、列挙の列挙子リストに含まれていない値を返す場合、これは未指定の動作です。
このルールに準拠することで、enum
型のオブジェクトには列挙子に対応する値のみが含まれるという、この規約の他のルールによる仮定が立証されます。
enum
型の式を評価するときに未指定の動作が発生しないようにするには、switch
ステートメントを使用します。以下に例を示します。
#include <cstdio> enum class E : size_t { Pass, Warning, Error }; E Evaluate(size_t input) noexcept { E output = E::Pass; switch (input) { case 0: { output = E::Pass; break; } case 1: { output = E::Warning; break; } case 2: { output = E::Error; break; } default: { output = static_cast<E>(0); } } return output; }
Polyspace 実装
Polyspace® は列挙の列挙子ではない値の使用にフラグを設定します。
enum
式で値を使用する前にその値の範囲をチェックしない場合でも、Polyspace は不明な値の使用にはフラグを設定しません。
トラブルシューティング
ルール違反が想定されるものの、Polyspace から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。
例
チェック情報
グループ: 宣言 |
カテゴリ: 必要 |
バージョン履歴
R2023a で導入
参考
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++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.