Initializing big class: I don't want any normal members initialized, but DO want to clear bit fields to 0

33 views Asked by At

Say I have this class and am happy with leaving all the non-bit field members with their garbage contents, but I need to set all the bit fields to 0. What is the most efficient way to achieve that? For instance, sould you expect an -O2 build of this would inline the memset() to be more efficient than an initializer list of bf1(0),bf2(0),...bf400(0)? Or at least same speed? Or is there another obvious way to do this?

Also if in addition I had 100 std::string is there any way to keep them from all initializing and just left as garbage values?

class Big {
Big() {
  memset( this + offsetof( this, psz300 ) + sizeof( psz300 ), '\0',
          sizeof( this ) - offsetof( this, psz300 ) - sizeof( psz300 ) );
};

double d1, ... d100;
int    i1, ... i100;
const char* psz1, ... * psz300;

unsigned long bf1:2,  ... bf400:2;
}

;

0

There are 0 answers