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 ?
Yes it is Undefined Behavior.
Reference:
C++03 Standard:
7.1.2 Function specifiers [dcl.fct.spec]
Para 4:
Note: 3.2 refers to the One Definition Rule, Which states:
3.2 One definition rule [basic.def.odr]
Para 1: