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; }
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist