...
Code Block | ||||
---|---|---|---|---|
| ||||
// S.h struct S { int a; }; // a.cpp #include "S.h" // b.cpp #include "S.h" |
Compliant Solution
If the ODR violation was a result of accidental name collision, the best mitigation solution is to ensure that both class definitions are unique, as in this compliant solution:
...
For more information on the behavior of #pragma pack
, see the vendor documentation for your implementation, such as Microsoft Visual Studio or GCC.
Compliant Solution
In this compliant solution, the implementation-defined structure member alignment directive is removed, ensuring that all definitions of S
comply with the ODR:
...
Code Block | ||||
---|---|---|---|---|
| ||||
const int n = 42; int g(const int &lhs, const int &rhs); inline int f(int k) { return g(k, n); } |
Compliant Solution
A compliant solution must change one of three factors: it must not odr-use n
within f()
, it must declare n
such that it has external linkage, or it must not use an inline definition of f()
.
...