Search code examples
c++clang-tidy

clang tidy pro type member init resharper


I've got some code parsing json like so:

  QJsonParseError errors;
  auto doc = QJsonDocument::fromJson(myJson.toUtf8(), &errors);

Resharper's clang tidy suggestions flags that QJsonParseError errors is an 'uninitialized record type'

The suggested fix is to zero initialize the variable via {} for C++11. The autofix offered by resharper, puts in some brackets like: QJsonParseError errors{};

What does that actually mean/do?


Solution

  • Zero initialization guarantees that the members of a class/struct are zero initialized. For example -

    struct student
    {
        int idNo;
        char name[20];
    };
    

    So, if object of student is zero-initialized, then it is guaranteed that the member variables idNo, name value(s) are initialized with zeroes (i.e., idNo = 0 and name array filled with zeroes).

    In your case, QJsonParseError members are zero initialized rather than filling with some random values during object initialization.