brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · 12aeaa8 Raw
82 lines · plain
1.. title:: clang-tidy - cppcoreguidelines-prefer-member-initializer2 3cppcoreguidelines-prefer-member-initializer4===========================================5 6Finds member initializations in the constructor body which can be  converted7into member initializers of the constructor instead. This not only improves8the readability of the code but also positively affects its performance.9Class-member assignments inside a control statement or following the first10control statement are ignored.11 12This check implements `C.4913<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c49-prefer-initialization-to-assignment-in-constructors>`_14from the C++ Core Guidelines.15 16Please note, that this check does not enforce rule `C.4817<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c48-prefer-in-class-initializers-to-member-initializers-in-constructors-for-constant-initializers>`_18from the C++ Core Guidelines. For that purpose19see check :doc:`modernize-use-default-member-init20<../modernize/use-default-member-init>`.21 22Example 123---------24 25.. code-block:: c++26 27  class C {28    int n;29    int m;30  public:31    C() {32      n = 1; // Literal in default constructor33      if (dice())34        return;35      m = 1;36    }37  };38 39Here ``n`` can be initialized in the constructor initializer list, unlike40``m``, as ``m``'s initialization follows a control statement (``if``):41 42.. code-block:: c++43 44  class C {45    int n;46    int m;47  public:48    C(): n(1) {49      if (dice())50        return;51      m = 1;52    }53  };54 55Example 256---------57 58.. code-block:: c++59 60  class C {61    int n;62    int m;63  public:64    C(int nn, int mm) {65      n = nn; // Neither default constructor nor literal66      if (dice())67        return;68      m = mm;69    }70  };71 72Here ``n`` can be initialized in the constructor initializer list, unlike73``m``, as ``m``'s initialization follows a control statement (``if``):74 75.. code-block:: c++76 77  C(int nn, int mm) : n(nn) {78    if (dice())79      return;80    m = mm;81  }82