メインコンテンツ

MISRA C++:2008 Rule 7-3-6

using-directives and using-declarations (excluding class scope or function scope using-declarations) shall not be used in header files

説明

ルール定義

using-directives and using-declarations (excluding class scope or function scope using-declarations) shall not be used in header files. 1

根拠

using 命令または宣言がヘッダー ファイル内に存在する場合は、ヘッダーのインクルード順序が最終的なプログラム結果に影響する可能性があります。プログラムの動作がヘッダーのインクルード順序に依存しないことを確認することをお勧めします。

Polyspace 実装

チェッカーは、ヘッダー ファイル内の using 命令または宣言にフラグを設定します。

トラブルシューティング

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

すべて展開する

ヘッダー ファイル: headerUnits.h:

void checkValueForOverflows(int32_t);

namespace Units {
    void checkValueForOverflows(int8_t);
}

inline void convertToAcceptedUnit(int8_t originalValue) {
    checkValueForOverflows(originalValue);
}

ヘッダー ファイル: headerAcceptedUnits.h:

namespace Units {
    
}

using namespace Units; //Noncompliant

上記 2 つのヘッダーをインクルードしたソース ファイル:

#include <cstdint>
#include "headerUnits.h"
#include "headerAcceptedUnits.h"

int8_t convert(int8_t meterReading) {
    convertToAcceptedUnit(meterReading);
}

この例では、ヘッダー ファイル headerAcceptedUnits.h 内の using 命令によって、名前空間 Units が公開されます。ヘッダー内のこの命令とインライン化された関数によって、headerUnits.hheaderAcceptedUnits.h のインクルード順序が、最終的なプログラム実行にとって重要になります。

  • headerAcceptedUnits.hheaderUnits.h より前にインクルードされている場合は、関数 convertToAcceptedUnit() が関数 checkValueForOverflows(int32_t) を呼び出します。

  • headerAcceptedUnits.hheaderUnits.h より後でインクルードされている場合は、関数 convertToAcceptedUnit() が関数 checkValueForOverflows(int8_t) を呼び出します。

チェック情報

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

バージョン履歴

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.