メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

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

A cast shall not convert a pointer to a function to any other pointer type, including a pointer to function type.

説明

ルール定義

A cast shall not convert a pointer to a function to any other pointer type, including a pointer to function type. 1

根拠

互換性のない型のポインターを介した関数呼び出しは未定義の動作を引き起こすため、ルールでは以下のようなキャスト操作が禁止されています。

  • 関数ポインターから別の型へのキャスト。

  • 関数ポインターから別の関数ポインターへのキャスト (それぞれの関数ポインターが異なる引数と戻り値の型をもつ場合)。

Polyspace 実装

Polyspace® は、キャスト操作のソース型が関数へのポインターである場合に、このルールの違反を報告します。

トラブルシューティング

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

すべて展開する

void* (*func1) (int);
void* (*func2)(float);
void bar(void* (*)(int));
void foo(){
	
	bar(reinterpret_cast<void* (*) (int)> (func2)); //Noncompliant
	bar(func1);
}

この例では、関数 bar が、入力として int を取得し、void* ポインターを返す関数へのポインターを受け入れています。foo() では、bar に渡すことができるように関数ポインター func2 が変換されます。Polyspace は、関数ポインターの非準拠の変換にフラグを設定します。

チェック情報

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

バージョン履歴

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.