Are functors actually faster than pointers to functions?

前端 未结 3 1017
刺人心
刺人心 2021-01-31 06:58

According to Scott Meyers, one area where C++ shines over C is that function objects are faster than function pointers. He says this is because function objects are inlined, whi

3条回答
  •  一向
    一向 (楼主)
    2021-01-31 07:56

    1. How can we verify that function objects are in fact inlined? Can we verify this in practice?

    Sure, inspect the finally emitted assembler code.

    1. The inlining function objects depends on the compiler we use, or all compilers behave like that?

    It heavily depends on compiler implementation and optimization level used.
    So no, there's no guarantee particular compilers (linkers) behave so.

    Calls through function pointers cannot be inlined though.


    According to him, function objects are inlined, so there is an increase in speed.

    IMO "function objects are inlined" should better read (or heard, I don't know where that cite is from):

    function objects can be inlined while calls through function pointers cannot.

提交回复
热议问题