Why can#39;t C++11 move a noncopyable functor to a std::function?(为什么 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 规范要求您的 A 是 CopyConstructible与 std::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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 C++11 不能将不可复制的函子移动到 std::function?
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
