Is it an undefined behavior to have different definitions of an inline function?

c++, function-prototypes, inline, undefined-behavior

Solution

Yes it is Undefined Behavior.

Reference:

C++03 Standard:

7.1.2 Function specifiers [dcl.fct.spec] Para 4:

An inline function shall be defined in every translation unit in which it is used and shall have exactly the same definition in every case (3.2). [Note: a call to the inline function may be encountered before its definition appears in the translation unit. ] If a function with external linkage is declared inline in one translation unit, it shall be declared inline in all translation units in which it appears; no diagnostic is required. An inline function with external linkage shall have the same address in all translation units. A static local variable in an extern inline function always refers to the same object. A string literal in an extern inline function is the same object in different translation units.

Note: 3.2 refers to the One Definition Rule, Which states:

3.2 One definition rule [basic.def.odr] Para 1:

No translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template.

Problem

Minimal code: ``` // --------inline.h-------- struct X { static inline void foo (); }; #ifdef YES inline void X::foo () { cout << "YES\n"; } #else inline void X::foo () { cout << "NO\n"; } #endif // --------file1.cpp-------- #define YES // <---- #include"inline.h" void fun1 () { X::foo(); } // --------file2.cpp-------- #include"inline.h" void fun2 () { X::foo(); } ``` If we call `fun1()` and `fun2()`, then they will print `YES` and `NO` respectively, which means they are referring different function bodies of same `X::foo()`. Irrespective of this should be coded or not, my question is: Is this a well defined or undefined behavior ?

Original source