Can C ++ modules fix ODR violations?

From the N4720 C ++ Modules Project, [basic.def.odr] / 6 says:

[...] For an object with an exported declaration, there must be only one definition of this object; diagnostics is required only if the abstract semantics column of the module contains the definition of the object, [Note: If the definition is not in the interface module, then no more than one module can use the definition. - end of note] [...]

Which, in my opinion, practically gives templates the ability to analyze only once by the compiler, unlike the current state of affairs (with an exact copy of their definitions per translation unit). This is also true for other objects with a similar case, such as built-in functions / variables.

My question arose because, since you can have at most one entity definition (as described in [basic.def.odr] / 1 ) for a translation unit, undefined behavior has different entity definitions through TU. And since exported objects should have only one definition in the whole compilation unit (which makes it impossible for a unique implementation unit), from my point of view, the definition is incorrect, more difficult if not impossible.

Finally, simply put: will (or does or should) the use of modules make it impossible to violate ODR rules or is it otherwise more difficult to make a mistake?

+5
source share
1 answer

If the project is completely modular (i.e. never uses #include ), then most of the random ODR violations will disappear. Most random ODR violations occur due to the nature of #include : including a global variable with definition, etc. Or two-phase problems of searching for a template, where two files contain the same template, but because each of them is included before this template, the definition of two templates is different.

However, this does nothing to prevent less "random" ODR violations. Since "the same object" is defined by its name, and the name of the object has nothing to do with the module from which it was exported, two modules can provide different definitions of the same object. So basically the names of the conflicts.

This becomes only a compilation (i.e. the required diagnostics) if one translation unit imports both of these modules. If two separate translation units include one of the modules with different definitions, then the program as a whole still violates ODR, but there is no need for diagnostics.

Thus, even in a fully modular code base, ODR can still be violated.

+3
source

Source: https://habr.com/ru/post/1275216/


All Articles