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

    1. <small id='tIajQ'></small><noframes id='tIajQ'>

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

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

        为什么 C++11 不能将不可复制的函子移动到 std::function?

        Why can#39;t C++11 move a noncopyable functor to a std::function?(为什么 C++11 不能将不可复制的函子移动到 std::function?)
      1. <legend id='JeERC'><style id='JeERC'><dir id='JeERC'><q id='JeERC'></q></dir></style></legend>
        <i id='JeERC'><tr id='JeERC'><dt id='JeERC'><q id='JeERC'><span id='JeERC'><b id='JeERC'><form id='JeERC'><ins id='JeERC'></ins><ul id='JeERC'></ul><sub id='JeERC'></sub></form><legend id='JeERC'></legend><bdo id='JeERC'><pre id='JeERC'><center id='JeERC'></center></pre></bdo></b><th id='JeERC'></th></span></q></dt></tr></i><div id='JeERC'><tfoot id='JeERC'></tfoot><dl id='JeERC'><fieldset id='JeERC'></fieldset></dl></div>

      2. <small id='JeERC'></small><noframes id='JeERC'>

          <tbody id='JeERC'></tbody>
        <tfoot id='JeERC'></tfoot>

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

                  本文介绍了为什么 C++11 不能将不可复制的函子移动到 std::function?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  //------------------------------------------------------------------------------
                  struct A
                  {
                      A(){}
                      A(A&&){}
                      A& operator=(A&&){return *this;}
                      void operator()(){}
                  
                  private:
                      A(const A&);
                      A& operator=(const A&);
                  
                      int x;
                  };
                  
                  //------------------------------------------------------------------------------
                  int main()
                  {
                      A a;
                      std::function<void()> func(std::move(a));
                  }
                  

                  'A::A' : 不能访问类 'A' 中声明的私有成员

                  'A::A' : cannot access private member declared in class 'A'

                  似乎当我通过引用或 const 捕获某些内容时,我可以创建一个不可复制的 lambda.但是,当我这样做时,它实际上可以将它提供给 std::function.

                  It seems like when I capture something by reference or const I can make a non-copyable lambda. However when I do that it actually works to give it to a std::function.

                  推荐答案

                  简短的回答是 C++11 规范要求您的 ACopyConstructiblestd::function 一起使用.

                  The short answer is that the C++11 specification requires your A to be CopyConstructible to be used with std::function.

                  长的答案是这个要求存在是因为 std::function 在构造函数中删除了你的函子的类型.为此,std::function 必须通过虚函数访问函子的某些成员.其中包括调用运算符、复制构造函数和析构函数.由于这些是通过虚拟调用访问的,因此无论您是否实际使用 std::function 的复制构造函数、析构函数或调用运算符,它们都会被使用".

                  The long answer is this requirement exists because std::function erases the type of your functor within the constructor. To do this, std::function must access certain members of your functor via virtual functions. These include the call operator, the copy constructor and the destructor. And since these are accessed via a virtual call, they are "used" whether or not you actually use std::function's copy constructor, destructor or call operator.

                  这篇关于为什么 C++11 不能将不可复制的函子移动到 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++程序中的堆栈溢出错误)
                1. <i id='drrif'><tr id='drrif'><dt id='drrif'><q id='drrif'><span id='drrif'><b id='drrif'><form id='drrif'><ins id='drrif'></ins><ul id='drrif'></ul><sub id='drrif'></sub></form><legend id='drrif'></legend><bdo id='drrif'><pre id='drrif'><center id='drrif'></center></pre></bdo></b><th id='drrif'></th></span></q></dt></tr></i><div id='drrif'><tfoot id='drrif'></tfoot><dl id='drrif'><fieldset id='drrif'></fieldset></dl></div>
                      • <bdo id='drrif'></bdo><ul id='drrif'></ul>

                        1. <tfoot id='drrif'></tfoot>
                          1. <small id='drrif'></small><noframes id='drrif'>

                              <tbody id='drrif'></tbody>
                            <legend id='drrif'><style id='drrif'><dir id='drrif'><q id='drrif'></q></dir></style></legend>