C and C++


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

Summary

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.

Properties

Class Name Out of Order Member Initializers
Significance style
Mnemonic LANG.STRUCT.INIT.OOMI
Categories
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.
Availability Available for C++ only (not C).
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Out of Order Member Initializers"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

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) 
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.