I was bitten by the unpleasant "one rule of definition." I am now afraid to have many subtle mistakes in my projects.
For example, the following program will dereference a null pointer with visual studio 2015:
Source1.cpp: ---------- struct S { double d = 0; }; void Foo() { S s; } Source2.cpp: ----------- struct S { int a = 0; }; int main() { int value = 5; int& valueRef = value; S s;
This compiles without warning.
This is nasty because Source2.cpp doesn't even use anything from Source1.cpp . If I remove Source1.cpp from the project, it still compiles and there are no more problems.
In large projects, it seems very difficult to ensure that not a single cpp file locally defines a structure or class with an already defined name.
I have some classes, such as Point , Serie , State , Item , ... I, although this was normal in small cpp files, but I understand that it is not safe.
Is there a compiler warning to catch such errors? If not, what are the best methods to avoid breaking ODR?
c ++ visual-c ++ visual-studio
ThreeStarProgrammer57
source share