<tfoot id='nUzj5'></tfoot>

      <i id='nUzj5'><tr id='nUzj5'><dt id='nUzj5'><q id='nUzj5'><span id='nUzj5'><b id='nUzj5'><form id='nUzj5'><ins id='nUzj5'></ins><ul id='nUzj5'></ul><sub id='nUzj5'></sub></form><legend id='nUzj5'></legend><bdo id='nUzj5'><pre id='nUzj5'><center id='nUzj5'></center></pre></bdo></b><th id='nUzj5'></th></span></q></dt></tr></i><div id='nUzj5'><tfoot id='nUzj5'></tfoot><dl id='nUzj5'><fieldset id='nUzj5'></fieldset></dl></div>

      <small id='nUzj5'></small><noframes id='nUzj5'>

        <legend id='nUzj5'><style id='nUzj5'><dir id='nUzj5'><q id='nUzj5'></q></dir></style></legend>
          <bdo id='nUzj5'></bdo><ul id='nUzj5'></ul>

      1. C++ 编译器可以内联函数指针吗?

        时间:2023-09-16
            • <bdo id='xMyhc'></bdo><ul id='xMyhc'></ul>
                <tbody id='xMyhc'></tbody>

                <small id='xMyhc'></small><noframes id='xMyhc'>

                <legend id='xMyhc'><style id='xMyhc'><dir id='xMyhc'><q id='xMyhc'></q></dir></style></legend>
                <tfoot id='xMyhc'></tfoot>
                  <i id='xMyhc'><tr id='xMyhc'><dt id='xMyhc'><q id='xMyhc'><span id='xMyhc'><b id='xMyhc'><form id='xMyhc'><ins id='xMyhc'></ins><ul id='xMyhc'></ul><sub id='xMyhc'></sub></form><legend id='xMyhc'></legend><bdo id='xMyhc'><pre id='xMyhc'><center id='xMyhc'></center></pre></bdo></b><th id='xMyhc'></th></span></q></dt></tr></i><div id='xMyhc'><tfoot id='xMyhc'></tfoot><dl id='xMyhc'><fieldset id='xMyhc'></fieldset></dl></div>
                  本文介绍了C++ 编译器可以内联函数指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  假设我有一个函数 functionProxy,它接受一个泛型参数 function 并调用它的 operator():

                  Suppose I've got a function functionProxy that takes a generic parameter function and call its operator():

                  template< typename Function > void functionProxy( Function function ) {
                      function();
                  }
                  

                  <小时>

                  传递给它的对象可能是:


                  The object passed to it may be:

                  • 一个函子:

                  • a functor:

                  struct Functor {
                      void operator()() const {
                          std::cout << "functor!" << std::endl;
                      }
                  };
                  

                1. 一个函数:

                2. a function:

                  void function( ) {
                      std::cout << "function!" << std::endl;
                  }
                  

                3. 一个 (C++0x) lambda 函数:

                4. a (C++0x) lambda function:

                  [](){ std::cout << "lambda!" << std::endl; }
                  

                5. int main( )
                  {
                      functionProxy( Functor() );
                      functionProxy( function );
                      functionProxy( [](){ std::cout << "lambda!" << std::endl; } );
                      return 0;
                  }
                  

                  <小时>

                  在上述所有情况下,编译器是否能够将 function 内联到 functionProxy 中?

                  推荐答案

                  没问题.

                  它知道function的值和它传递给它的值是一样的,知道函数的定义,所以直接内联替换定义,直接调用函数.

                  It knows the value of function is the same as the value it passes it, knows the definition of the function, so just replaces the definition inline and calls the function directly.

                  我想不出编译器不会内联单行函数调用的情况,它只是用函数调用替换函数调用,没有可能的损失.

                  I can't think of a condition where a compiler won't inline a one-line function call, it's just replacing a function call with a function call, no possible loss.

                  鉴于此代码:

                  #include <iostream>
                  
                  template <typename Function>
                  void functionProxy(Function function)
                  {
                      function();
                  }
                  
                  struct Functor
                  {
                      void operator()() const
                      {
                          std::cout << "functor!" << std::endl;
                      }
                  };
                  
                  void function()
                  {
                      std::cout << "function!" << std::endl;
                  }
                  
                  //#define MANUALLY_INLINE
                  
                  #ifdef MANUALLY_INLINE
                  void test()
                  {
                      Functor()();
                  
                      function();
                  
                      [](){ std::cout << "lambda!" << std::endl; }();
                  }
                  #else
                  void test()
                  {
                      functionProxy(Functor());
                  
                      functionProxy(function);
                  
                      functionProxy([](){ std::cout << "lambda!" << std::endl; });
                  }
                  #endif
                  
                  int main()
                  {
                      test();
                  }
                  

                  定义 MANUALLY_INLINE 后,我们得到:

                  With MANUALLY_INLINE defined, we get this:

                  test:
                  00401000  mov         eax,dword ptr [__imp_std::endl (402044h)]  
                  00401005  mov         ecx,dword ptr [__imp_std::cout (402058h)]  
                  0040100B  push        eax  
                  0040100C  push        offset string "functor!" (402114h)  
                  00401011  push        ecx  
                  00401012  call        std::operator<<<std::char_traits<char> > (401110h)  
                  00401017  add         esp,8  
                  0040101A  mov         ecx,eax  
                  0040101C  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (40204Ch)]  
                  00401022  mov         edx,dword ptr [__imp_std::endl (402044h)]  
                  00401028  mov         eax,dword ptr [__imp_std::cout (402058h)]  
                  0040102D  push        edx  
                  0040102E  push        offset string "function!" (402120h)  
                  00401033  push        eax  
                  00401034  call        std::operator<<<std::char_traits<char> > (401110h)  
                  00401039  add         esp,8  
                  0040103C  mov         ecx,eax  
                  0040103E  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (40204Ch)]  
                  00401044  mov         ecx,dword ptr [__imp_std::endl (402044h)]  
                  0040104A  mov         edx,dword ptr [__imp_std::cout (402058h)]  
                  00401050  push        ecx  
                  00401051  push        offset string "lambda!" (40212Ch)  
                  00401056  push        edx  
                  00401057  call        std::operator<<<std::char_traits<char> > (401110h)  
                  0040105C  add         esp,8  
                  0040105F  mov         ecx,eax  
                  00401061  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (40204Ch)]  
                  00401067  ret  
                  

                  没有,这个:

                  test:
                  00401000  mov         eax,dword ptr [__imp_std::endl (402044h)]  
                  00401005  mov         ecx,dword ptr [__imp_std::cout (402058h)]  
                  0040100B  push        eax  
                  0040100C  push        offset string "functor!" (402114h)  
                  00401011  push        ecx  
                  00401012  call        std::operator<<<std::char_traits<char> > (401110h)  
                  00401017  add         esp,8  
                  0040101A  mov         ecx,eax  
                  0040101C  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (40204Ch)]  
                  00401022  mov         edx,dword ptr [__imp_std::endl (402044h)]  
                  00401028  mov         eax,dword ptr [__imp_std::cout (402058h)]  
                  0040102D  push        edx  
                  0040102E  push        offset string "function!" (402120h)  
                  00401033  push        eax  
                  00401034  call        std::operator<<<std::char_traits<char> > (401110h)  
                  00401039  add         esp,8  
                  0040103C  mov         ecx,eax  
                  0040103E  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (40204Ch)]  
                  00401044  mov         ecx,dword ptr [__imp_std::endl (402044h)]  
                  0040104A  mov         edx,dword ptr [__imp_std::cout (402058h)]  
                  00401050  push        ecx  
                  00401051  push        offset string "lambda!" (40212Ch)  
                  00401056  push        edx  
                  00401057  call        std::operator<<<std::char_traits<char> > (401110h)  
                  0040105C  add         esp,8  
                  0040105F  mov         ecx,eax  
                  00401061  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (40204Ch)]  
                  00401067  ret
                  

                  一样.(与 MSVC 2010 一起编译,原版.)

                  The same. (Compiled with MSVC 2010, vanilla Release.)

                  这篇关于C++ 编译器可以内联函数指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:禁用 g++ 的返回值优化 下一篇:布尔值在编译器中为 8 位.对它们的操作效率低下

                  相关文章

                  最新文章

                  • <bdo id='pL75M'></bdo><ul id='pL75M'></ul>

                  1. <legend id='pL75M'><style id='pL75M'><dir id='pL75M'><q id='pL75M'></q></dir></style></legend>

                    <small id='pL75M'></small><noframes id='pL75M'>

                    <tfoot id='pL75M'></tfoot>

                    1. <i id='pL75M'><tr id='pL75M'><dt id='pL75M'><q id='pL75M'><span id='pL75M'><b id='pL75M'><form id='pL75M'><ins id='pL75M'></ins><ul id='pL75M'></ul><sub id='pL75M'></sub></form><legend id='pL75M'></legend><bdo id='pL75M'><pre id='pL75M'><center id='pL75M'></center></pre></bdo></b><th id='pL75M'></th></span></q></dt></tr></i><div id='pL75M'><tfoot id='pL75M'></tfoot><dl id='pL75M'><fieldset id='pL75M'></fieldset></dl></div>