3 ms·
Explicitly defaulting the constructor behaves the same as an empty constructor: all members are default-initialized. The problem is usually that 'default-initia
by typelist 10y ago
Explicitly defaulting the constructor behaves the same as an empty constructor: all members are default-initialized. The problem is usually that 'default-initializing' doesn't zero out primitive (POD) types, but leaves them uninitialized.
- CamperBob2 10y agoThe problem is usually that 'default-initializing' doesn't zero out primitive (POD) types, but leaves them uninitialized. Exactly, that's what hosed me. Some double-precision floats ended up with garbage values and I couldn't see how it was happening.
- gpderetta 10y agoExplicitly using the T(), T{}, T = {} syntax will value-initialize the object, which will zero-initialize trivially constructible types.
- AndrewGaspar 10y agoYes, forgot to mention this crucial detail.
- typelist 10y agoAh, interesting. Apparently this is true even if some constructors are explicitly '= default' (for the T x{} and T x={} list/aggregate initialization cases only). But I believe that rule doesn't apply for any objects where adding an '= default' constructor would be useful, because it requires that there be no user-defined constructors or private/protected members. Otherwise that syntax ultimately calls the default constructor, which won't do any zeroing. See http://en.cppreference.com/w/cpp/language/aggregate_initialization http://en.cppreference.com/w/cpp/language/aggregate_initiali...
- ryuuchin 10y agoWith msvc the /sdl flag can zero initialize class members to zero[1] (among other things). This runs before the constructor. Aside from the warnings that get turned into errors (you can disable the warning to bypass this behavior) it also does limited pointer sanitization and turns on strict_gs_check. [1] https://msdn.microsoft.com/en-us/library/jj161081.aspx https://msdn.microsoft.com/en-us/library/jj161081.aspx