If I make a function inline does it change its linkage to internal linkage? For example:
I’m using or calling the inline function in two files:
file1.cpp
//function definition
inline void foo() {}
file2.cpp
//function definition
inline void foo() {}
Why do I need to define the inline function in each file to be able to call it? I’m getting an internal linkage? What if I use static inline?
No, making it inline does not change its linkage.
Because the language requires it. C++11 7.1.2/4 says “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.”
No, it still has external linkage: a pointer to the function will have the same value in any translation unit, and any static objects declared inside the function will be the same object in any translation unit.
That will give internal linkage, due to the
static. This means that definitions in separate translation units will produce separate functions, with different addresses and distinct copies of any static objects.