I was wondering if it's possible for C++ compilers to optimize in a case like this. Assume we have a class like:
class Foo {
public:
Foo() : a(10), b(11), c(12) {};
int a;
int b;
int c;
};
And I use this class like:
int main(int argc, char *argv[]) {
Foo f;
f.a = 50;
f.b = 51;
f.c = 52;
return 0;
}
Would the compiler generate code to set a, b, and c to their respective default values of 10, 11, 12, and then set them to 50, 51, 52? Or is it allowed to delay assigning those initial values and instead only ever assign the values later (50,51,52) since there is no read in between the writes? Basically will it have to generate code to write those six values, or can it optimize to three?
If so, does this apply also to more complex types (structs, classes)? What is it called and where can I read more about this?
If not, why not?