<legend id='3cbX1'><style id='3cbX1'><dir id='3cbX1'><q id='3cbX1'></q></dir></style></legend>

        • <bdo id='3cbX1'></bdo><ul id='3cbX1'></ul>

        <small id='3cbX1'></small><noframes id='3cbX1'>

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

        std::function 是如何实现的?

        时间:2023-06-30

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

          • <tfoot id='xRA9T'></tfoot>
              <bdo id='xRA9T'></bdo><ul id='xRA9T'></ul>

                <legend id='xRA9T'><style id='xRA9T'><dir id='xRA9T'><q id='xRA9T'></q></dir></style></legend>
                <i id='xRA9T'><tr id='xRA9T'><dt id='xRA9T'><q id='xRA9T'><span id='xRA9T'><b id='xRA9T'><form id='xRA9T'><ins id='xRA9T'></ins><ul id='xRA9T'></ul><sub id='xRA9T'></sub></form><legend id='xRA9T'></legend><bdo id='xRA9T'><pre id='xRA9T'><center id='xRA9T'></center></pre></bdo></b><th id='xRA9T'></th></span></q></dt></tr></i><div id='xRA9T'><tfoot id='xRA9T'></tfoot><dl id='xRA9T'><fieldset id='xRA9T'></fieldset></dl></div>
                    <tbody id='xRA9T'></tbody>
                  本文介绍了std::function 是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  根据我找到的资料,lambda 表达式 本质上是由编译器实现的,该类使用重载的函数调用运算符和引用的变量作为成员.这表明 lambda 表达式的大小各不相同,并且给定足够的引用变量,大小可以任意大.

                  According to the sources I have found, a lambda expression is essentially implemented by the compiler creating a class with overloaded function call operator and the referenced variables as members. This suggests that the size of lambda expressions varies, and given enough references variables that size can be arbitrarily large.

                  std::function 应该具有固定大小,但它必须能够包装任何类型的可调用对象,包括任何同类的 lambda.它是如何实施的?如果 std::function 在内部使用指向其目标的指针,那么当 std::function 实例被复制或移动时会发生什么?是否涉及任何堆分配?

                  An std::function should have a fixed size, but it must be able to wrap any kind of callables, including any lambdas of the same kind. How is it implemented? If std::function internally uses a pointer to its target, then what happens, when the std::function instance is copied or moved? Are there any heap allocations involved?

                  推荐答案

                  std::function 的实现可以从一个实现到另一个不同,但核心思想是它使用类型擦除.虽然有多种方法可以做到这一点,但您可以想象一个简单的(不是最佳的)解决方案可能是这样的(针对 std::function 的特定情况进行了简化)为了简单起见):

                  The implementation of std::function can differ from one implementation to another, but the core idea is that it uses type-erasure. While there are multiple ways of doing it, you can imagine a trivial (not optimal) solution could be like this (simplified for the specific case of std::function<int (double)> for the sake of simplicity):

                  struct callable_base {
                     virtual int operator()(double d) = 0;
                     virtual ~callable_base() {}
                  };
                  template <typename F>
                  struct callable : callable_base {
                     F functor;
                     callable(F functor) : functor(functor) {}
                     virtual int operator()(double d) { return functor(d); }
                  };
                  class function_int_double {
                     std::unique_ptr<callable_base> c;
                  public:
                     template <typename F>
                     function(F f) {
                        c.reset(new callable<F>(f));
                     }
                     int operator()(double d) { return c(d); }
                  // ...
                  };
                  

                  在这个简单的方法中,function 对象将只存储一个 unique_ptr 到一个基本类型.对于与 function 一起使用的每个不同的函子,会创建一个从基派生的新类型,并动态实例化该类型的对象.std::function 对象始终具有相同的大小,并将根据需要为堆中的不同函子分配空间.

                  In this simple approach the function object would store just a unique_ptr to a base type. For each different functor used with the function, a new type derived from the base is created and an object of that type instantiated dynamically. The std::function object is always of the same size and will allocate space as needed for the different functors in the heap.

                  在现实生活中,有不同的优化可以提供性能优势,但会使答案复杂化.类型可以使用小对象优化,动态分派可以用一个自由函数指针代替,该指针以函子为参数以避免一级间接......但想法基本相同.

                  In real life there are different optimizations that provide performance advantages but would complicate the answer. The type could use small object optimizations, the dynamic dispatch can be replaced by a free-function pointer that takes the functor as argument to avoid one level of indirection... but the idea is basically the same.

                  关于 std::function 副本的行为问题,快速测试表明内部可调用对象的副本已完成,而不是共享状态.

                  Regarding the issue of how copies of the std::function behave, a quick test indicates that copies of the internal callable object are done, rather than sharing the state.

                  // g++4.8
                  int main() {
                     int value = 5;
                     typedef std::function<void()> fun;
                     fun f1 = [=]() mutable { std::cout << value++ << '
                  ' };
                     fun f2 = f1;
                     f1();                    // prints 5
                     fun f3 = f1;
                     f2();                    // prints 5
                     f3();                    // prints 6 (copy after first increment)
                  }
                  

                  测试表明 f2 获取可调用实体的副本,而不是引用.如果可调用实体由不同的 std::function<> 对象共享,则程序的输出将是 5、6、7.

                  The test indicates that f2 gets a copy of the callable entity, rather than a reference. If the callable entity was shared by the different std::function<> objects, the output of the program would have been 5, 6, 7.

                  这篇关于std::function 是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么 C++11 的 lambda 需要“可变"?默认情况下,按值捕获的关键字? 下一篇:如何将 unique_ptr 捕获到 lambda 表达式中?

                  相关文章

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

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

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

                    1. <tfoot id='Zx1np'></tfoot>