One definition rule warning
<p>I have been bitten by a nasty "one definition rule" violation.
I am now afraid of having lots of subtle bugs in my projects.</p>

<p>For example, the following program will result in a null pointer dereference with visual studio 2015:</p>

<pre><code>Source1.cpp:
----------
struct S {
    double d = 0;
};

void Foo() {
    S s;
}


Source2.cpp:
-----------
struct S {
    int a = 0;
};

int main() {

    int value = 5;
    int&amp; valueRef = value;
    S s;           // valueRef is erased due to S::d initialization from Source1.cpp

    valueRef++;    // crash
}
</code></pre>

<p>This compiles with no warnings.</p>

<p>It's nasty because <code>Source2.cpp</code> doesn't even use anything from <code>Source1.cpp</code>. If I remove <code>Source1.cpp</code> from the project, it still compiles, and there is no problem anymore.</p>

<p>In large projects, it seems very hard to ensure that no cpp file "locally" define a struct or class with an already defined name. </p>

<p>I have some classes such as <code>Point</code>, <code>Serie</code>, <code>State</code>, <code>Item</code>, ... I though this was OK in small cpp files, but I realize it's not safe.</p>

<p>Is there a compiler warning to catch such bugs ?
If not, what are the best practices to avoid ODR violation ?</p>