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
?
If I make a function inline does it change its linkage to internal linkage?
No, making it inline does not change its linkage.
Why do I need to define the inline function in each file to be able to call it?
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."
I'm getting an internal linkage?
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.
What if I use
static inline
?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With