为什么我不能在 Objective-C 中使用 boost::function ?堵塞?

发布于 2024-10-27 16:04:42 字数 1024 浏览 3 评论 0原文

抛出异常

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_function_call> >'
  what():  call to empty boost::function

以下代码在 f() 行(执行块时)

void foo(); // assume this is defined somewhere
boost::function<void()> f = boost::bind(&foo);

^(void) {
   f();
}();

:但是,根据 关于块的文档

一般来说,您可以在块中使用 C++ 对象。在成员函数中,对成员变量和函数的引用是通过隐式导入的 this 指针实现的,因此看起来是可变的。如果复制块,则需要考虑两个注意事项:

  • 如果您有一个用于基于堆栈的 C++ 对象的 __block 存储类,则使用通常的复制构造函数。

  • 如果您在块内使用任何其他基于 C++ 堆栈的对象,则它必须具有 const 复制构造函数。然后使用该构造函数复制 C++ 对象。

通常情况下这似乎是正确的;如果我将上面的 f 替换为带有 operator()() 的简单类的实例,上面的代码将按预期运行。

为什么带有 boost::function 的版本不起作用?

The following code throws an exception

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_function_call> >'
  what():  call to empty boost::function

at the line f() (while executing the block):

void foo(); // assume this is defined somewhere
boost::function<void()> f = boost::bind(&foo);

^(void) {
   f();
}();

However, according to the documentation on blocks,

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.

This seems to be true normally; if I replace f above with an instance of a simple class with an operator()(), the above code runs as expected.

Why doesn't the version with boost::function work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅士风度i 2024-11-03 16:04:43

看来,如果我用 __block 修改声明,它会正常工作:

__block boost::function<void()> f = boost::bind(&foo);

我仍然不确定为什么会这样 - 正如 @Richard 在上面的评论中提到的,它必须与“const复制构造函数”而不是“通常的复制构造函数”。但我不知道如何检查这种差异;以下工作正常:

const boost::function<void()> f = boost::bind(&foo);
const boost::function<void()> g(f);
g();

如果这不调用“const 复制构造函数”,我不确定会调用什么。

It appears that if I modify the declaration with __block, it works correctly:

__block boost::function<void()> f = boost::bind(&foo);

I'm still not sure why this is - as @Richard mentions in a comment above, it must have to do with the "const copy constructor" as opposed to the "usual copy constructor". I don't know how to check this difference, though; the following works fine:

const boost::function<void()> f = boost::bind(&foo);
const boost::function<void()> g(f);
g();

and if that doesn't call a "const copy constructor", I'm not sure what will.

忆离笙 2024-11-03 16:04:43

您答案中的示例

const boost::function<void()> f = boost::bind(&foo);
const boost::function<void()> g(f);
g();

之所以有效,是因为您在创建其被调用者的同一范围内调用 operator()f() 也可以在块作用域之外工作,调用 foo() 也可以在块作用域内工作,因为它是一个经典函数,不被认为是“修改的” “当它被调用时。将对象从包含作用域传递到块作用域时,会进行对 const 复制构造函数的引用调用。看来 const 复制构造函数中存在的任何差异都无法将对象的一部分复制到块作用域。

The example in your answer,

const boost::function<void()> f = boost::bind(&foo);
const boost::function<void()> g(f);
g();

works because you're calling operator() in the same scope in which its callee was created. f() would work outside of block scope as well, and calling foo() would work inside block scope because it is a classic function and is not considered to be "modified" when it is called. The referenced call to a const copy constructor is made when passing the object from containing scope to block scope. It would appear that whatever difference exists in the const copy constructor is failing to copy part of the object through to block scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文