# Adding in-class \`{}\` default member initializers to data members of ITK classes

**URL:** https://discourse.itk.org/t/adding-in-class-default-member-initializers-to-data-members-of-itk-classes/5613
**Category:** Engineering
**Created:** [January 5, 2023, 11:34am UTC](https://discourse.itk.org/t/adding-in-class-default-member-initializers-to-data-members-of-itk-classes/5613 "2023-01-05T11:34:54Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [January 5, 2023, 11:34am UTC](https://discourse.itk.org/t/adding-in-class-default-member-initializers-to-data-members-of-itk-classes/5613/1 "2023-01-05T11:34:54Z")

</div>

Generally speaking, it appears hard to avoid undefined behavior when data members of classes aren’t consistently being initialized. We saw a few cases recently:

- [Fix MSVC warning C26495: `ZwQuerySystemInformation`, `m_MetaDataObjectValue` uninitialized by N-Dekker · Pull Request #3843 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/pull/3843)
- [BUG: Fix uninitialized value `ImageRegistrationMethodv4::m_NumberOfLevels` by jhlegarreta · Pull Request #3845 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/pull/3845)
- [COMP: Fix MSVC warning C26495: `m_PositionIndex` uninitialized by N-Dekker · Pull Request #3849 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/pull/3849)

Hereby I’m proposing to consistently use in-class default member initializers for data members of ITK classes “by default”, and _only_ leave data members uninitialized in very specific cases (specific cases, typically where the run-time performance cost of initialization would be too high).

For those data members that did not yet have an in-class default member initializer, the following PR proposes to add a `{}` initializer:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/3851>
>
> For an object created by \`New()\`, the performance cost of adding a \`{}\` default …member initializer to each of its non-static data members should be neglectable compared to the cost of allocating the object itself, as it is allocated on the heap.
> 
> In-class default member initialization effectively prevents Valgrind/Memcheck warnings like "Conditional jump or move depends on uninitialised value(s)", and Visual C++ Code Analysis warnings like "warning C26495: Variable is uninitialized. Always initialize a member variable (type.6)"
> 
> These cases are found using the regular expression \` \\w.\* m\_\\w\[^{}=\]+;\`, excluding \`static\` data members and data members of a reference (\`&\`) type.

The PR is limited to classes whose instances are created by `New()`, assuming that for those classes, the cost of the added `{}` initialization is neglectable compared to the cost of object creation. Please have a look: [STYLE: Add in-class `{}` member initializers to objects created by New() by N-Dekker · Pull Request #3851 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/pull/3851)
