The ODR allows us to define several times the same inline function (with some restrictions).
However, what about the simpler case of static
functions?
// First TU
static int foo() { return 0; }
int bar1() { return foo(); }
// Second TU
static int foo() { return 1; }
int bar2() { return foo(); }
If we do a quick read of [basic.def.odr]p4, we could naively conclude that this would be UB:
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.
Where in the C++ standard is specified that each foo
is a different function and therefore not breaking the ODR, even if they have the same name?
Is it simply a matter of reading [basic.link]p2.2 (i.e. due to internal linkage the names do not refer to the same entity and therefore [basic.def.odr]p4 does not apply here)? Or are there more nuances/rules involved to make this determination (like something in [basic.scope])?
Note that, with unnamed namespaces, the outcome is clear, because the name would be already different/unique.
static
specifier, and the linker took on the first definition found from another TU, that was added later to the project :). I've even posted a not so well received quesiton after we found out. – Staff