search

Home  >  Q&A  >  body text

C++函数很短, 但是函数内调用一个很长的函数, 该用inline吗?

void x(){
    thisIsALongFunction();
}

如上图所示的代码, x()中只有一句话, 但是它所调用的函数很长, 这种情况下需要inline吗?

高洛峰高洛峰2813 days ago605

reply all(5)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 14:29:40

    In this case, the compiler will directly inline the outer function.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 14:29:40

    inlineThis is just a suggestion to the compiler, not a requirement.

    However, the implementation of mainstream compilers will ignore your suggestions. If you turn on O2, short functions will be automatically inlined, but long functions will not be inlined. So ignore it.

    reply
    0
  • 阿神

    阿神2017-04-17 14:29:40

    Add it, inlining will remove your extra layer of encapsulation, and the encapsulation inside should still be there

    reply
    0
  • 黄舟

    黄舟2017-04-17 14:29:40

    Don’t worry about these issues, it doesn’t matter whether the code optimized by the compiler is inline or not.

    reply
    0
  • PHPz

    PHPz2017-04-17 14:29:40

    First figure out what inline is for,

    reply
    0
  • Cancelreply