...
Alternatively, the classes could be given distinct names in each translation unit to avoid violating the ODR.
Noncompliant Code Example (Microsoft Visual Studio)
In this noncompliant code example, a class definition is introduced into two translation units using #include
. However, one of the translation units uses a common, an implementation-defined #pragma
that is supported by Microsoft Visual Studio to specify structure field alignment requirements. Consequently, the two class definitions may have differing layouts in each translation unit, which is a violation of the ODR.
...
Page properties | ||
---|---|---|
| ||
I am uncertain whether it would be interesting or not, but another NCCE/CS pair that is specific to Microsoft Visual Studio would be the generic text mappings use by a lot of Win32 APIs (and Windows code in general). The IDE gives you a flag that you can toggle that specifies whether
I hesitate to add this as an NCCE/CS pair because it's so implementation-specific and I think the point is already made with other examples in this rule. However, this is one of those scenarios that can bite Win32 programmers if they're not observant, and the flag is relatively hidden. |
Noncompliant Code Example
In this noncompliant code example, the constant object n
has internal linkage but is odr-used within f()
, which has external linkage. Because f()
is declared as an inline function, the definition of f()
must be identical in all translation units. However, each translation unit has a unique instance of n
, resulting in a violation of the ODR.
...