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

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

        为什么 std::function 的初始化程序必须是可复制构

        时间:2023-05-25

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

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

              • <small id='IxneN'></small><noframes id='IxneN'>

              • <i id='IxneN'><tr id='IxneN'><dt id='IxneN'><q id='IxneN'><span id='IxneN'><b id='IxneN'><form id='IxneN'><ins id='IxneN'></ins><ul id='IxneN'></ul><sub id='IxneN'></sub></form><legend id='IxneN'></legend><bdo id='IxneN'><pre id='IxneN'><center id='IxneN'></center></pre></bdo></b><th id='IxneN'></th></span></q></dt></tr></i><div id='IxneN'><tfoot id='IxneN'></tfoot><dl id='IxneN'><fieldset id='IxneN'></fieldset></dl></div>
                • 本文介绍了为什么 std::function 的初始化程序必须是可复制构造的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  根据http://en.cppreference.com/w/cpp/utility/functional/function/function,初始化器的类型,即形式(5)中的F,应满足CopyConstructible的要求.我不太明白这个.为什么 F 不能只是 MoveConstructible?

                  According to http://en.cppreference.com/w/cpp/utility/functional/function/function, the type of the initializer, i.e., F in form (5), should meet the requirements of CopyConstructible. I don't quite get this. Why is it not OK for F to be just MoveConstructible?

                  推荐答案

                  std::function 在内部使用类型擦除,因此 F 必须是可复制构造的,即使您使用的特定 std::function 对象从未被复制.

                  std::function uses type erasure internally, so F has to be CopyConstructible even if the particular std::function object you are using is never copied.

                  类型擦除工作原理的简化:

                  A simplification on how type erasure works:

                  class Function
                  {
                      struct Concept {
                          virtual ~Concept() = default;
                          virtual Concept* clone() const = 0;
                          //...
                      }
                  
                      template<typename F>
                      struct Model final : Concept {
                  
                          explicit Model(F f) : data(std::move(f)) {}
                          Model* clone() const override { return new Model(*this); }
                          //...
                  
                          F data;
                      };
                  
                      std::unique_ptr<Concept> object;
                  
                  public:
                      template<typename F>
                      explicit Function(F f) : object(new Model<F>(std::move(f))) {}
                  
                      Function(Function const& that) : object(that.object->clone()) {}
                      //...
                  
                  };
                  

                  您必须能够生成 Model::clone(),这会强制 F 为 CopyConstructible.

                  You have to be able to generate Model<F>::clone(), which forces F to be CopyConstructible.

                  这篇关于为什么 std::function 的初始化程序必须是可复制构造的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:混合模板与多态性 下一篇:为什么两个函数的地址相同?

                  相关文章

                  最新文章

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

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

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

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