C and C++


LANG.FUNCS.ICS : Implicit Constructor Shadowing

要旨

The definition of a constructor for class or struct C has all of the following properties.

A constructor with these properties is a copy constructor for C.

プロパティ

クラス名 Implicit Constructor Shadowing
日本語クラス名 Implicit Constructor Shadowing
クラス分類 スタイル (style)
ニーモニック LANG.FUNCS.ICS
カテゴリー
MisraC++2023 MisraC++2023:15.0.2 User-provided copy and move member functions of a class should have appropriate signatures
CWE CWE:1076 Insufficient Adherence to Expected Conventions
JSF++ JSF++:77.1 The definition of a member function shall not contain default arguments that produce a signature identical to that of the implicitly-declared copy constructor for the corresponding class/structure.
対応言語 C++ のみ利用可能です。 C は利用できません。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Implicit Constructor Shadowing"
注:非正規化された AST を継続して使用した場合、使用ディスク容量が増加し解析時間が長くなる可能性があります。

struct NoImpl {
    // This is a defining declaration for the copy constructor,
    // so there is no implicitly defined copy constructor.
    NoImpl(const NoImpl& x, int i=0){}                       // 'Implicit Constructor Shadowing' warning issued here
};

struct ImplShadowed {
    // There IS an implicitly defined copy constructor for this class,
    // because this declaration does not indicate the defaulted parameter.
    ImplShadowed(const ImplShadowed& x, int i);
};

// Definition matches signature of copy constructor, even though declaration doesn't.
// (parse error)
ImplShadowed::ImplShadowed(const ImplShadowed& x, int i=0){} // 'Implicit Constructor Shadowing' warning issued here

struct ImplOk {
    // There IS an implicitly defined copy constructor for this class,
    // but it is not shadowed by the definition of this constructor.
    ImplOk(const ImplOk& x, int i);                                    // Ok: no user confusion or parse issues. 
};

// Constructor definition does not have type signature matching that of the copy constructor.
ImplOk::ImplOk(const ImplOk& x, int i){}  

void my_init(NoImpl x, ImplShadowed y, ImplOk z){
    // Calls the explicitly declared/defined copy constructor.
    // This may not be what a human reader expects.
    auto ni_copy = NoImpl(x);            

    // Resolution conflict: matches both implicitly defined copy constructor and explicit definition.
    // (parse error)
    auto ishadow_copy = ImplShadowed(y);  

    // Calls the implicitly declared/defined copy constructor.
    auto iok_copy = ImplOk(z);            
}

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。