Is there a compiler warning under gcc and/or vs to detect member variables initiated to themselves?
gcc has -Winit-self, but only seems to work for
int f()
{
int i = i;
return i;
}
and not for
class A {
int m;
public:
A(int) : m(m) { }
int f() {return m;}
};
Edit: Take the question back, all that was missing was an -O1 or above (Thanks @honk)
Edit 2: Actually, the problem is back on the table. In a simplistic example -O1 -Wuninitialized -Winit-self works, however, it catches it not at the point where you declare m(m) but rather when you define A(4). This also means that the compiler doesn’t pick up on it if the constructor is in its own compilation unit (which I would imagine should be rather often in real world scenarios).
There don’t seem to be any compiler flags either under msvc or gcc. However, with gcc, valgrind picks up on any use of uninitiated variables, which sorts the problem. Not certain of a solution for Windows. Visual Studio Code Analysis looks like it might pick it up, but that is no available in the base versions.