I accidentally messed up a c++ member value definition:
struct Composite
{
std::shared_ptr<MyStruct> member = member = std::make_shared<MyStruct>();
// ^^^^^^
};
I only discovered the code after some segmentation fault error in code that was copy-pasting the above; I still don't know if the segfault was due to the above. To my surprise such code compiles, and as shown in the linked demo, it seems to work fine (although I cannot guarantee that). My question is:
- Is this valid code?
- If not, what is the language specification that bans it?
- If yes, how on earth is it legal to refer to yourself during definition? The object doesn't yet exist, so isn't there a recursive definition error involved. Curiously, such code is even possible during non member variable definition like
int k = k;, so it's not a weird rule about scope inside a class. Is this some omission in the language specification, or is it intentional?