I've just found a pretty dangerous bug in my code, and I feel like it should have been caught by the compiler. Am I wrong? In essence, a reference member of a class is allowed to be initialized by itself in the constructor. Here's the test code that compiles in Visual Studio 2017 without errors or warnings:
struct foo
{
foo() : reference(reference) {}
int& reference;
};
int main()
{
foo fooOb;
}
UPDATE: I see that there is a similar question from 2009 here. Do other compilers behave the same in 2017, or is it a VS 2017 issue? If they do, it kinda suggests to me that this is legal C++, but I don't see how it could be.