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

2021-12-23 00:00:00 function lambda c++ c++11
//------------------------------------------------------------------------------
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.

相关文章