static vs inline for functions implemented in header files

后端 未结 4 1207
心在旅途
心在旅途 2021-02-01 18:20

The way I think of inline in C++ is for linkage/scoping. I put it in the same basket with extern and static for global objects.

Ty

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-01 18:53

    In many cases you will not notice a difference because compilers and linkers are pretty smart these days. However, an inline function must behave as-if it was a regular function. A static function in a header will get compiled into every source file which includes it - so there will be lots of copies of it.

    Mostly, this doesn't matter much, but there are a few ways it does. An inline function has one address. Static functions will have a different address in each translation unit.

    Static-local variables: WIth the inline, there will be a single copy of them. With static-functions, there will be a unique copy of each static-local variable for each translation unit that includes that function.

提交回复
热议问题