c++ - 如何 boost::bind 以通用引用作为参数的模板成员函数

标签 c++ c++11 templates boost universal-reference

我一直在尝试使用 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模板函数来处理“完美捕获”。您可以在这些资源中找到有关如何实现的信息:

通过组合上述资源,您应该能够在 C++11 中实现“完美的捕获包装器”。

您还需要一个 forward_like 辅助函数来保留 a 参数的原始值类别。你可以找到一个实现:

关于c++ - 如何 boost::bind 以通用引用作为参数的模板成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41856449/

相关文章:

c++ - 这是数组初始化吗?

C++ : Error while replacing character

c++ - 为什么不能引用指针以用于常量分配字符串文字

java - 不使用算术运算符的乘法、除法和平方根

C++ 11 避免 "Call Super"代码异味

c++ - 模板化 Sum(Args...) 可变参数函数无法编译

c++ - 过滤嵌套动态元组(dynamic tuple of tuples)

c++ - 没有原始指针返回二进制数据的通用方法

c++ - std::chrono::high_resolution_clock 不准确?

正在缓存的 Django 自定义模板标签