What Does It Mean For a C++ Function To Be Inline?

后端 未结 9 794
攒了一身酷
攒了一身酷 2020-12-01 06:12

See title: what does it mean for a C++ function to be inline?

9条回答
  •  青春惊慌失措
    2020-12-01 07:09

    It means one thing and one thing only: that the compiler will elide multiple definitions of the function.

    A function normally cannot be defined multiple times (i.e. if you place a non-inline function definition into a header and then #include it into multiple compilation units you will receive a linker error). Marking the function definition as "inline" suppresses this error (the linker ensures that the Right Thing happens).

    IT DOES NOT MEAN ANYTHING MORE!

    Most significantly, it does NOT mean that the compiler will embed the compiled function into each call site. Whether that occurs is entirely up to the whims of the compiler, and typically the inline modifier does little or nothing to change the compiler's mind. The compiler can--and does--inline functions that aren't marked inline, and it can make function calls to functions that are marked inline.

    Eliding multiple definitions is the thing to remember.

提交回复
热议问题