c++ - Default value of members in a struct -
in struct
struct { int a; a(){ } }; a; is specified value has a.a? doe have ub if try read a.a?
if a a; has static storage duration (like defining outside main()), or thread storage duration (i.e. defined thread_local in c++11 or later), a zero-initialized (thanks @praetorian comment)
3.6.2/2 [basic.start.init] variables static storage duration (3.7.1) or thread storage duration (3.7.2) shall zero-initialized (8.5) before other initialization takes place.
in case, means each member of object a a zeroed, hence a.a zeroed, constructor a() run (which won't anything). @ end of day, a.a zero.
if a a has non-static/non-thread storage duration (e.g. having a a; inside function), no 0 initialization performed, , again constructor doesn't anything. have ub (undefined behaviour) if try read a.a, since latter left un-initialized.
Comments
Post a Comment