メインコンテンツ

MISRA C++:2008 Rule 9-6-2

Bit-fields shall be either bool type or an explicitly unsigned or signed integral type.

説明

ルール定義

Bit-fields shall be either bool type or an explicitly unsigned or signed integral type. 1

根拠

ビット フィールドを使用するには、その基となるビット表現が処理系定義ではないことが必要です。bool 型、signed 整数型、および unsigned 整数型以外の型では、基となるビット表現は明示的に既知ではありません。たとえば、int ビット フィールドの基となる表現は、実装に応じて符号付きも符号なしもあり得ます。同様に、wchar_t 型の基となるビット表現の符号属性は ISO/IEC 14882:2003 で明確に定義されていません。

bool 型、signed 整数型、および unsigned 整数型以外の型をビット フィールドとして使用すると、実装依存の方法で動作するコードが作成されたり、診断が難しいバグが発生したりする可能性があります。ビット フィールドを使用するときには、bool 型、signed 整数型、または unsigned 整数型を使用してください。

Polyspace 実装

ビット フィールドの型が次のいずれかの場合、Polyspace® はこのルールの違反を報告します。

  • 明示的な符号指定がない整数型

  • wchar_t

ビット フィールドの型が次のいずれかの場合、Polyspace はこの違反を報告しません。

  • bool

  • 明示的に符号付きまたは明示的に符号なしの整数型

  • 明示的に符号付きまたは明示的に符号なしの char

トラブルシューティング

ルール違反が想定されるものの、Polyspace から報告されない場合は、コーディング規約違反が想定どおりに表示されない理由の診断を参照してください。

すべて展開する

#include <cstdint>
struct S
{
	signed int sInt_f : 2; // Compliant
	unsigned int uInt_f : 2; // Compliant
	char Ch_f : 2; // Noncompliant
	signed char sCh_f : 2; // Compliant
	unsigned char uCh_f : 2; // Compliant
	short Sh_f : 2; // Noncompliant
	signed short sSh_f : 2; // Compliant
	unsigned short uSh_f : 2; // Compliant
	int Int_f : 2; // Noncompliant
	bool Bool_f : 2; // Compliant
	wchar_t wch_f : 2; // Noncompliant
	int32_t sInt32_f : 2; // Noncompliant
	int8_t sInt8_f : 2; // Compliant
	long Long_f:2; //Noncompliant
	unsigned long uLong_f:2; //Compliant
};

この例では、符号付きまたは符号なしが明示的に指定されていない整数型ビット フィールドと、wchar_t 型ビット フィールドに対し、Polyspace がフラグを設定しています。

チェック情報

グループ: Classes
カテゴリ: 必要

バージョン履歴

R2013b で導入


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.