devxlogo

Understanding the One Definition Rule

Understanding the One Definition Rule

The One Definition Rule (ODR) says that an inline function has to be identically defined in every translation unit in which it is used. Two definitions contained in different translation units are identically defined if and only if they are token-by-token identical, and the tokens have identical meaning in both translation units. For example:

   //file fisrt.cppinline int C::getVal () { return 5; }   //file sec.cpptypedef int I;inline I C::getVal () { return 5; } //violation of ODR, I and int are not identical tokens

On the other hand, white spaces and comments are immaterial:

   //file fisrt.cppinline int C::getVal () { return 5; }   //file sec.cppinline int C::getVal () { /*complies with the ODR*/return 5; }

devx-admin

Share the Post: