Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does making a function inline affect its linkage?

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?

like image 557
user1086635 Avatar asked Dec 22 '11 14:12

user1086635


1 Answers

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.

like image 63
Mike Seymour Avatar answered Nov 08 '22 08:11

Mike Seymour