R
rajkumar
I have a struct like
struct MyStruct
{
int a;
int b;
int c:
bool d;
bool e;
}
I want to insert such a struct in a map. I understand I can declare the
< operator for such a struct for lexicographical compare like
x.a < y.a || !(y.a < x.a) && ( x.b < y.b || !(y.b < x.b) && x.c < y.c
.............
or a simple one like this
memcmp(&x,&y,sizeof(MyStruct)) < 0;
This seems to work if I memset and fill the sizeof(MyStruct) with
zeroes in the constructor before I assign a b and c etc. This will take
care of any padding that the compiler adds.
My question is whether the second approach is portable? If so do I
really need the memset ? Does the standard say anything about
initializing the padding bits?
Raj
struct MyStruct
{
int a;
int b;
int c:
bool d;
bool e;
}
I want to insert such a struct in a map. I understand I can declare the
< operator for such a struct for lexicographical compare like
x.a < y.a || !(y.a < x.a) && ( x.b < y.b || !(y.b < x.b) && x.c < y.c
.............
or a simple one like this
memcmp(&x,&y,sizeof(MyStruct)) < 0;
This seems to work if I memset and fill the sizeof(MyStruct) with
zeroes in the constructor before I assign a b and c etc. This will take
care of any padding that the compiler adds.
My question is whether the second approach is portable? If so do I
really need the memset ? Does the standard say anything about
initializing the padding bits?
Raj