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

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

      • <bdo id='mBRdZ'></bdo><ul id='mBRdZ'></ul>
    3. 在 C++ 中键入擦除:boost::shared_ptr 和 boost::function 如何工作?

      Type erasure in C++: how boost::shared_ptr and boost::function work?(在 C++ 中键入擦除:boost::shared_ptr 和 boost::function 如何工作?)
        <legend id='zh5Zt'><style id='zh5Zt'><dir id='zh5Zt'><q id='zh5Zt'></q></dir></style></legend>
      1. <i id='zh5Zt'><tr id='zh5Zt'><dt id='zh5Zt'><q id='zh5Zt'><span id='zh5Zt'><b id='zh5Zt'><form id='zh5Zt'><ins id='zh5Zt'></ins><ul id='zh5Zt'></ul><sub id='zh5Zt'></sub></form><legend id='zh5Zt'></legend><bdo id='zh5Zt'><pre id='zh5Zt'><center id='zh5Zt'></center></pre></bdo></b><th id='zh5Zt'></th></span></q></dt></tr></i><div id='zh5Zt'><tfoot id='zh5Zt'></tfoot><dl id='zh5Zt'><fieldset id='zh5Zt'></fieldset></dl></div>
                <tbody id='zh5Zt'></tbody>

              <tfoot id='zh5Zt'></tfoot>

                <bdo id='zh5Zt'></bdo><ul id='zh5Zt'></ul>

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

              • 本文介绍了在 C++ 中键入擦除:boost::shared_ptr 和 boost::function 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                类型擦除 - 你是这么称呼它的吗?

                Type erasure - is that how you call it?

                boost::shared_ptr 如何存储它的删除器以及 boost::function 如何存储它的函数对象?

                How boost::shared_ptr stores its deleter and how boost::function stores its function object?

                有教这个技巧的教程吗?

                Is there any tutorial that teaches the trick?

                使用类型擦除函数对象的运行时成本是多少?

                What is the run-time cost of using type-erased function objects?

                推荐答案

                想法很简单,您定义一个基类,该基类具有包含您需要的功能的接口,然后从它继承.由于type erased 类只使用该接口,下面的实际类型是forgottenerased.或者,如果唯一需要的接口可以表示为自由函数,您可以存储指向自由函数的指针.

                The idea is simple, you define a base class that has an interface with the functionality you need, and then inherit from it. Since the type erased class uses only that interface, the actual type underneath is forgotten and erased. Alternatively, if the only needed interface can be expressed as free functions you can store pointers to the free functions.

                namespace detail {
                   struct deleter_base {
                      virtual ~deleter_base() {}
                      virtual void operator()( void* ) = 0;
                   };
                   template <typename T>
                   struct deleter : deleter_base {
                      virtual void operator()( void* p ) {
                         delete static_cast<T*>(p);
                      }
                   };
                }
                template <typename T>
                class simple_ptr {
                   T* ptr;
                   detail::deleter_base* deleter;
                public:
                   template <typename U>
                   simple_ptr( U* p ) {
                      ptr = p;
                      deleter = new detail::deleter<U>();
                   }
                   ~simple_ptr() {
                      (*deleter)( ptr );
                      delete deleter;
                   }
                };
                

                这是一个真正简化的智能指针,但想法就在那里.在 shared_ptr 的特殊情况下,deleter 作为引用计数对象的一部分存储,由指针保存.

                This is a really simplified smart pointer, but the idea is there. In the particular case of shared_ptr, the deleter is stored as part of the reference count object, that is held by pointer.

                这篇关于在 C++ 中键入擦除:boost::shared_ptr 和 boost::function 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                How boost auto-linking makes choice?(boost自动链接如何做出选择?)
              • <i id='jkkcH'><tr id='jkkcH'><dt id='jkkcH'><q id='jkkcH'><span id='jkkcH'><b id='jkkcH'><form id='jkkcH'><ins id='jkkcH'></ins><ul id='jkkcH'></ul><sub id='jkkcH'></sub></form><legend id='jkkcH'></legend><bdo id='jkkcH'><pre id='jkkcH'><center id='jkkcH'></center></pre></bdo></b><th id='jkkcH'></th></span></q></dt></tr></i><div id='jkkcH'><tfoot id='jkkcH'></tfoot><dl id='jkkcH'><fieldset id='jkkcH'></fieldset></dl></div>

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

                      <bdo id='jkkcH'></bdo><ul id='jkkcH'></ul>
                      <tfoot id='jkkcH'></tfoot>

                        • <legend id='jkkcH'><style id='jkkcH'><dir id='jkkcH'><q id='jkkcH'></q></dir></style></legend>
                            <tbody id='jkkcH'></tbody>