gpt4 book ai didi

objective-c - Objective-C++ block 语义

转载 作者:太空狗 更新时间:2023-10-30 03:28:49 26 4
gpt4 key购买 nike

考虑以下 C++ 方法:

class Worker{
....
private Node *node
};

void Worker::Work()
{
NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock: ^{
Tool hammer(node);
hammer.Use();
}];
....
}

区 block 捕获“节点”时,究竟捕获了什么? block 的语言规范,http://clang.llvm.org/docs/BlockLanguageSpec.html , 对于其他情况很清楚:

Variables used within the scope of the compound statement are bound to the Block in the normal manner with the exception of those in automatic (stack) storage. Thus one may access functions and global variables as one would expect, as well as static local variables. [testme]

Local automatic (stack) variables referenced within the compound statement of a Block are imported and captured by the Block as const copies.

但是在这里,我们是否捕获了 this 的当前值?使用 Worker 的复制构造函数复制 this?还是对存储节点的地方的引用?

特别地,假设我们说

 {
Worker fred(someNode);
fred.Work();
}

当 block 运行时,对象 fred 可能不再存在。 节点的值(value)是多少? (假设底层 Node 对象永远存在,但 Workers 来来去去。)

如果我们写成

void Worker::Work()
{
Node *myNode=node;
NSBlockOperation *op=[NSBlockOperation blockOperationWithBlock: ^{
Tool hammer(myNode);
hammer.Use();
}];
....
}

结果有什么不同吗?

最佳答案

根据 this page :

In general you can use C++ objects within a block. Within a member function, references to member variables and functions are via an implicitly imported this pointer and thus appear mutable. There are two considerations that apply if a block is copied:

  • If you have a __block storage class for what would have been a stack-based C++ object, then the usual copy constructor is used.
  • If you use any other C++ stack-based object from within a block, it must have a const copy constructor. The C++ object is then copied using that constructor.

根据经验,我观察到它 const 复制了 this指向 block 的指针。如果 this 指向的 C++ 实例当 block 执行时不再位于该地址(例如,如果调用 Worker::Work() 的 Worker 实例被堆栈分配在更高的帧上),那么您将获得 EXC_BAD_ACCESS 或更糟的信息(即指针别名)。所以看起来:

  • 正在捕获 this ,而不是按值复制实例变量。
  • 未采取任何措施来保持 this 指向的对象活着。

或者,如果我引用一个本地堆栈分配的(即在此堆栈帧/范围中声明的)C++ 对象,我观察到它的复制构造函数在它最初被 block 捕获时被调用,然后在 block 被捕获时再次调用复制(例如,在您将操作入队时由操作队列复制。)

具体解决您的问题:

But here, do we capture the current value of this? A copy of this using Worker’s copy constructor? Or a reference to the place where node is stored?

我们捕获 this .将其视为 intptr_t 的常量副本如果有帮助的话。

The object fred may not exist any more when the block gets run. What is the value of node? (Assume that the underlying Node objects live forever, but Workers come and go.)

在这种情况下,this已按值捕获并且 node实际上是一个值为 this + <offset of node in Worker> 的指针但由于 Worker 实例消失了,它实际上是一个垃圾指针。

除了这些文档中描述的内容外,我不会推断出任何魔法或其他行为。

关于objective-c - Objective-C++ block 语义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18493370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com