Re:The C++ Build Process Explained
by alexhutcheson on Thursday November 15, 2018 @10:53AM
(#18460213)
The functions can also be different if different compilation units were compiled using different compiler settings (or event different compilers).
In Titus Winters' recent Pacific++ talk[1], he pointed out that even something as simple as including an 'assert' statement will violate the ODR if some compilation units are compiled with 'debug' settings and some aren't. This can easily happen with build systems that cache compiled object files if changes in compilations flags don't automatically invalidate the cache.
> even if the text of a function is identical in different translation units, it can still be ODR-different between them if the symbols that they look up are different due to other header files included before them declaring different thing
Exactly. This is why you should never put anonymous namespaces and definitions of objects with static linkage in an header file.
> this causes undefined behaviour and compilers/linkers aren't required to issue a diagnostic (and they usually don't!)
-fsanitize=undefined with LTO has shown ODR violations for a long time
This sounds like it would be easy – just put the definition a header file. But even if the text of a function is identical in different translation units, it can still be ODR-different between them if the symbols that they look up are different due to other header files included before them declaring different thing or doing things like "using namespace std". Argument-dependent lookup is especially dangerous here. As with other ODR violations, this causes undefined behaviour and compilers/linkers aren't required to issue a diagnostic (and they usually don't!). I believe C++20 modules will solve this problem.