<bdo id='MnbqC'></bdo><ul id='MnbqC'></ul>
    1. <tfoot id='MnbqC'></tfoot>

      <legend id='MnbqC'><style id='MnbqC'><dir id='MnbqC'><q id='MnbqC'></q></dir></style></legend>

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

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

        我应该复制一个 std::function 还是我可以总是引用它?

        Should I copy an std::function or can I always take a reference to it?(我应该复制一个 std::function 还是我可以总是引用它?)
        <tfoot id='EHD5G'></tfoot>
            <legend id='EHD5G'><style id='EHD5G'><dir id='EHD5G'><q id='EHD5G'></q></dir></style></legend>
            <i id='EHD5G'><tr id='EHD5G'><dt id='EHD5G'><q id='EHD5G'><span id='EHD5G'><b id='EHD5G'><form id='EHD5G'><ins id='EHD5G'></ins><ul id='EHD5G'></ul><sub id='EHD5G'></sub></form><legend id='EHD5G'></legend><bdo id='EHD5G'><pre id='EHD5G'><center id='EHD5G'></center></pre></bdo></b><th id='EHD5G'></th></span></q></dt></tr></i><div id='EHD5G'><tfoot id='EHD5G'></tfoot><dl id='EHD5G'><fieldset id='EHD5G'></fieldset></dl></div>
              <tbody id='EHD5G'></tbody>

                <bdo id='EHD5G'></bdo><ul id='EHD5G'></ul>
              • <small id='EHD5G'></small><noframes id='EHD5G'>

                • 本文介绍了我应该复制一个 std::function 还是我可以总是引用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的 C++ 应用程序(使用 Visual Studio 2010)中,我需要存储一个 std::function,如下所示:

                  In my C++ application (using Visual Studio 2010), I need to store an std::function, like this:

                  class MyClass
                     {
                     public:
                        typedef std::function<int(int)> MyFunction;
                        MyClass (Myfunction &myFunction);
                     private:
                        MyFunction m_myFunction;    // Should I use this one?
                        MyFunction &m_myFunction;   // Or should I use this one?
                     };
                  

                  如您所见,我在构造函数中添加了函数参数作为引用.

                  As you can see, I added the function argument as a reference in the constructor.

                  但是,在我的类中存储函数的最佳方式是什么?

                  But, what is the best way to store the function in my class?

                  • 由于 std::function 只是一个函数指针并且保证函数的可执行代码"保留在内存中,我可以将函数存储为引用吗?
                  • 如果传递了 lambda 并且调用者返回,我是否必须复制一份?

                  我的直觉是存储引用(甚至是常量引用)是安全的.我希望编译器在编译时为 lambda 生成代码,并在应用程序运行时将此可执行代码保存在虚拟"内存中.因此,永远不会删除"可执行代码,我可以安全地存储对它的引用.但这真的是真的吗?

                  My gut feeling says that it's safe to store a reference (even a const-reference). I expect the compiler to generate code for the lambda at compile time, and keep this executable code in 'virtual' memory while the application is running. Therefore the executable code is never 'deleted' and I can safely store a reference to it. But is this really true?

                  推荐答案

                  我可以将函数存储为引用,因为 std::function 只是一个函数指针并且函数的可执行代码"保证保留在内存中?

                  Can I store the function as a reference since std::function is just a function-pointer and the 'executable code' of the function is guaranteed to stay in memory?

                  std::function 不仅仅是一个函数指针.它是任意可调用对象的包装器,并管理用于存储该对象的内存.与任何其他类型一样,仅当您有其他方法来保证引用的对象在使用该引用时仍然有效时,才可以安全地存储引用.

                  std::function is very much not just a function pointer. It's a wrapper around an arbitrary callable object, and manages the memory used to store that object. As with any other type, it's safe to store a reference only if you have some other way to guarantee that the referred object is still valid whenever that reference is used.

                  除非你有一个很好的理由来存储一个引用,并且有一种方法来保证它保持有效,否则按值存储它.

                  Unless you have a good reason for storing a reference, and a way to guarantee that it remains valid, store it by value.

                  const 引用传递给构造函数是安全的,并且可能比传递值更有效.传递非const 引用是个坏主意,因为它会阻止你传递一个临时的,所以用户不能直接传递一个lambda,bind 的结果,或除 std::function 本身之外的任何其他可调用对象.

                  Passing by const reference to the constructor is safe, and probably more efficient than passing a value. Passing by non-const reference is a bad idea, since it prevents you from passing a temporary, so the user can't directly pass a lambda, the result of bind, or any other callable object except std::function<int(int)> itself.

                  这篇关于我应该复制一个 std::function 还是我可以总是引用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                      <bdo id='tE2zH'></bdo><ul id='tE2zH'></ul>
                    • <tfoot id='tE2zH'></tfoot>
                        <tbody id='tE2zH'></tbody>
                      • <legend id='tE2zH'><style id='tE2zH'><dir id='tE2zH'><q id='tE2zH'></q></dir></style></legend>

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

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