Search code examples
c++c++17one-definition-rule

Why isn't the one definition rule abandoned for C++17?


Citing C++ Draft N4713:

Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program outside of a discarded statement (9.4.1); no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 15.1, 15.4 and 15.8). An inline function or variable shall be defined in every translation unit in which it is odr-used outside of a discarded statement.

In C++ versions prior to C++17, I can get around this restriction by just declaring my functions inline. C++17 adds the same feature for variables.

Furthermore, it seems to me that the inline-Keyword does not serve another purpose apart from making it possible to ignore the ODR.

So, why isn't this whole rule just abandoned for C++17 ? I can't see the purpose of a rule that can be turned off.


Solution

  • "Turning off" the ODR with inline is not free: the definition of an inline entity must be present in every translation unit. Note that this implies that any change to its definition causes re-compilation of every compilation unit that uses it. This would be particularly unpleasant when the function is part of some library many / big projects depend upon.

    Non-inline functions, on the other hand, live in just one compilation unit and are referenced via some symbol by the linker when needed elsewhere. Complying with the ODR guarantees that symbol is not ambiguous.