C and C++


LANG.STRUCT.INIT.OOMI : Out of Order Member Initializers

要旨

The ordering in the member initializer list for a class constructor does not match the order in which the initializers will be invoked.

The order of invocation for member initializers is specified in the C++ standard and is independent of their order in the initializer list. A warning of this class is issued if a constructor for class C is invoked at least once in the analyzed code but the member initializer list for the constructor does not meet all of the following requirements.

A member initializer list that does not reflect this ordering may cause confusion for human readers.

プロパティ

クラス名 Out of Order Member Initializers
日本語クラス名 Out of Order Member Initializers
クラス分類 スタイル (style)
ニーモニック LANG.STRUCT.INIT.OOMI
カテゴリー
AUTOSARC++14 AUTOSARC++14:A8-5-1 In an initialization list, the order of initialization shall be following: (1) virtual base classes in depth and left to right order of the inheritance graph, (2) direct base classes in left to right order of inheritance list, (3) non-static data members in the order they were declared in the class definition.
CERT-CPP CERT-CPP:OOP53-CPP Write constructor member initializers in the canonical order
JSF++ JSF++:75 Members of the initialization list shall be listed in the order in which they are declared in the class.
  JSF++:210 Algorithms shall not make assumptions concerning how data is represented in memory (e.g. big endian vs. little endian, base class subobject ordering in derived classes, nonstatic data member ordering across access specifiers, etc.)
  JSF++:210.1 Algorithms shall not make assumptions concerning the order of allocation of nonstatic data members separated by an access specifier.
対応言語 C++ のみ利用可能です。 C は利用できません。
有効/無効設定 このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Out of Order Member Initializers"
注:非正規化された AST を継続して使用した場合、使用ディスク容量が増加し解析時間が長くなる可能性があります。

namespace lang_struct_init_oomi{

  class VirtualBase {
  public:
    VirtualBase(void): second_int(1), // 'Out of Order Member Initializers' warning issued here
                       first_int(2) {};
    VirtualBase(int i){}
  private:
    int first_int;
    int second_int;
  };

  class FirstClass {
  public:
    FirstClass(void){}
    FirstClass(int i){}
  };

  class SecondClass {
  public:
    SecondClass(void){}
    SecondClass(int i){}
  };

  class Derived:  virtual VirtualBase,  FirstClass,  SecondClass {
  public:
    Derived(void) : mychar('x'),      // 'Out of Order Member Initializers' warning issued here
                    FirstClass(1),    // 'Out of Order Member Initializers' warning issued here
                    VirtualBase(2) {}

    Derived(int i) : SecondClass(i),  // 'Out of Order Member Initializers' warning issued here
                     FirstClass(i) {}

    Derived(char c) : VirtualBase(1),               // ok: 
                      FirstClass(1),                //   all initializer list ordering requirements
                      SecondClass(1),               //   met for this constructor
                      mychar(c) {}
  private:
    char mychar;
  };

  Derived d1;                                       // invokes Derived::Derived(void) 
  Derived d2(2);                                    // invokes Derived::Derived(int i), which in turn invokes VirtualBase::VirtualBase(void) 
  Derived d3('c');                                  // invokes Derived::Derived(char c) 
}

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

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