Different definitions of inline functions ?

If a inline function declaration is provided in the header file then every translation unit should contain this function's declaration and they should be the same, as I get it.

But what happens if the definitions are different amongst translation units ? Does every translation unit use it's version of the inline function, or does only one version get compiled and linked (so we can't be really sure which one will end up being called) ?

Maybe I'm just bad at googling but I couldn't find info about it anywhere.
C++11 ยง7.1.2/4
An inline function shall be defined in every translation unit in which it is odr-used and shall have exactly the same definition in every case

If you don't follow the language rules you can't expect anything. What will happen depends on how the compiler handles it, but you can't be sure the result is consistent. Changes to the code or different compiler settings might give you a different result.
what happens if the definitions are different amongst translation units ?
Standard says the results are undefined. That means your programm can transform computer into a robot and take over the world, format your hard drive or simply crash: all those are allowed behaviors in this case.

What exactly happens is up to compiler. In reality, some places would use own version of function, others (even within same translation unit) would use some common function. THere can be even several groups of "common" functions. So code
1
2
std::cout << bad_inline_function() << '\n';
std::cout << bad_inline_function() << '\n';
might produce different results for those two lines if there is several different definitions avaliable.
Last edited on
Topic archived. No new replies allowed.