c++ inline function?

前端 未结 6 479
终归单人心
终归单人心 2020-11-27 12:49

Why should i do something like this:

inline double square (double x) { return x*x;}

instead of

double square (double x) {          


        
6条回答
  •  醉话见心
    2020-11-27 13:33

    The former (using inline) allows you to put that function in a header file, where it can be included in multiple source files. Using inline makes the identifier in file scope, much like declaring it static. Without using inline, you would get a multiple symbol definition error from the linker.

    Of course, this is in addition to the hint to the compiler that the function should be compiled inline into where it is used (avoiding a function call overhead). The compiler is not required to act upon the inline hint.

提交回复
热议问题