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

2023-06-30C/C++开发问题
4

本文介绍了为什么 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6