Reputation: 63
Have a look at this code snippet
struct S{ int i; int j;};
int main()
{
assert(S().i == S().j) // is it guaranteed ?
}
Why?
Upvotes: 6
Views: 248
Reputation: 15872
Technically, yes. They will be initialized to 0 (at least under a non-debug build for most compilers. Visual Studio's compiler will usually initialize uninitialized variables to a specific pattern in debug builds).
However, if you were sitting in a code review, don't be surprised if you get yelled at for not explicitly initializing your variables.
Upvotes: -1
Reputation: 6181
From C++ Standard ISO/IEC 14882:2003(E) point 3.6.2
Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place.
So this is valid as both variables are zero-initialized.
Upvotes: 0
Reputation: 92854
is it guaranteed ?
Yes it is guaranteed. The values of S().i
and S().j
would be 0
. ()
implies value initialization. (that means i
and j
would be zero-initialized because S
is a class without a user defined default constructor)
Upvotes: 10