我一直在尝试使用 boost::bind 将对成员函数的调用发布到 io_strand 上,但一直出现错误。我设法创建了一个简单的等效示例来说明我正在尝试做的事情,并且在以下上下文中看到了相同的错误:
我有以下包含我要调用的 doThings() 成员函数的类:
class base
{
public:
int x = 1;
template<typename B>
void doThings(B&& b)
{}
};
然后有这个的一个子类(准确地表示我遇到错误的场景 - 我认为这没有什么区别)
class child : public base
{
int y = 2;
};
我有以下代码试图进行 boost::bind 调用:
template<typename A>
void getInt(child c, A&& a)
{
boost::bind((void(child::*)(A)) &child::doThings, &c, std::forward<A>(a))();
}
然后调用如下:
int main()
{
child c = child();
getInt(c, 7);
}
当我编译上面的代码时,出现以下错误:
error: no matches converting function ‘doThings’ to type ‘void (class child::*)(int)’
如果我将 doThings() 的函数签名更改为采用常规 B 类型而不是通用引用,即
B
而不是 B&&
然后它编译运行没有问题。
我怀疑我的问题与我在 getInt() 中所做的转换有关:
(void(child::*)(A))
但我不知道我需要将其更改为什么。 A&&
在这种情况下不起作用,因为我相信它会代表这种情况下的右值引用。我尝试时遇到的编译错误似乎证实了这一点:
error: cannot bind ‘int’ lvalue to ‘int&&’
为了完整起见:如果我不尝试执行转换,则会出现以下编译错误:
error: no matching function for call to ‘bind(unresolved overloaded function type, child*, int)’
有人可以告诉我我需要做什么才能使我的 boost::bind 调用在这种情况下有效吗?
我正在使用 C++11
最佳答案
我建议不要使用 boost::bind
,因为 lambda expressions可用于干净地绑定(bind)参数(避免在 this talk by STL 中解释的 bind
的许多陷阱)。
我假设你想要:
如果 rvalue-reference 被传递给
getInt
,则通过移动捕获a
。如果 lvalue-reference 传递给
getInt
,则通过引用捕获a
。
我还假设:
A
在您的真实代码中不是int
,否则完美转发将没有意义。您想避免
a
的不必要拷贝,或者A
可能是仅移动 类型。您只能访问 C++11 (而不是更新的标准)。
如果您需要“完美捕获” a
(即,如果 A
是右值,则通过移动捕获-引用,如果 A
是左值引用,则按引用捕获),您需要某种包装器。
不幸的是,这很重要,即使它在 C++14 和 C++17 中变得更好。下面是最终语法可能的示例:
template<typename A>
void getInt(child c, A&& a)
{
// `a_wrapper` stores an `A&` or an `A` depending on the value category
// of `a`. Copying it should not copy `a` - it should conditionally move
// it depending on the original value category of `a`.
auto a_wrapper = forward_capture_wrapper(std::forward<A>(a));
// `forward_like` retains information about `a`'s value category so that
// it can be used in the body of the lambda to forward the reference/value
// stored inside `a_wrapper`.
// vvvvvvvvvvvvvvv
[&a, a_wrapper]{ c.doThings(forward_like<A>(a_wrapper.get()); }();
// ^^^^^^^^^^^^^^^
// `a_wrapper.get()` returns a reference that can then be moved or passed
// to `c.doThings`.
}
如您所见,您需要一个名为 forward_capture_wrapper
的模板函数来处理“完美捕获”。您可以在这些资源中找到有关如何实现的信息:
"Move capture in lambda" - 涵盖如何在 C++11 中实现移动捕获。由于广义的 lambda 捕获,这在 C++14 中是微不足道的。
"capturing perfectly-forwarded objects in lambdas" - 这是我写的一篇文章,介绍了如何在 C++14/17 中实现“完美捕获”。这是理解“完美捕获”问题的一个很好的起点,应该可以将代码转换为符合 C++11 的代码。
"Capturing perfectly-forwarded variable in lambda" - 我前段时间提出的这个 StackOverflow 问题涵盖了在 C++14 中实现“完美捕获”的一些方法。
通过组合上述资源,您应该能够在 C++11 中实现“完美的捕获包装器”。
您还需要一个 forward_like
辅助函数来保留 a
参数的原始值类别。你可以找到一个实现:
关于c++ - 如何 boost::bind 以通用引用作为参数的模板成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41856449/