简体   繁体   English

将rvalue引用传递给boost :: in_place函数

[英]passing rvalue reference into boost::in_place function

I'm new to rvalue references in C++ and want to learn how to use them in everyday life. 我是C ++中rvalue引用的新手,想学习如何在日常生活中使用它们。 I've got 2 connected questions about popular usecases: using rvalue references with boost::in_place and boost::bind. 我有两个关于流行用例的连接问题:使用带有boost :: in_place的rvalue引用和boost :: bind。

  1. Using rvalue ref in boost::in_place 在boost :: in_place中使用rvalue ref

Consider a class, with constructor taking rvalue reference as a parameter: 考虑一个类,构造函数将rvalue引用作为参数:

struct A
    : boost::noncopyable 
{
    A(int&&){}
};

Let's now try to create boost optional variable for this class: 现在让我们尝试为这个类创建boost可选变量:

void foo(int&& value)
{
    boost::optional<A> opt;
    // some code here 
    opt = boost::in_place(std::forward<int>(value)); //Error!
}

What is the right way to pass rvalue ref in such an example. 在这样的例子中传递rvalue ref的正确方法是什么。 Is there any solution like boost::reference_wrapper for rvalue refs? rvalue refs有没有像boost :: reference_wrapper这样的解决方案?

  1. passing bind functor to function object with rvalue reference 使用右值引用将绑定仿函数传递给函数对象

Another common usecase is assigning boost::bind functor object to boost::function object. 另一个常见用例是将boost :: bind仿函数对象赋值给boost :: function对象。

void foo(int&&)
{
}

void bar()
{
    boost::function<void(int&&)> func;

    int x = 0;
    func = boost::bind(foo, std::move(x)); // Compilation error (a)

    func = boost::bind(foo, _1); // Compilation error too (b)
}

I understand that instruction (a) could lead to undefined variable value after first invocation, but instruction (b) even has no such a problem. 我理解指令(a)可能在第一次调用后导致未定义的变量值,但指令(b)甚至没有这样的问题。 But how to write this code correctly? 但是如何正确编写此代码?

boost in_places does not natively support move or r-value references. boost in_places本身不支持移动或r值引用。 However, you can make a new class that inherits from boost::in_place_factory_base, and make your own. 但是,您可以创建一个继承自boost :: in_place_factory_base的新类,并创建自己的类。 Or you could use an optional that supports emplace functions (like folly::Optional). 或者您可以使用支持emplace函数的可选项(如folly :: Optional)。

I'm not sure I recommend doing it, but you can make a generic overload in boost to handle it, something like this: 我不确定我是否建议这样做,但你可以在boost中进行泛型重载来处理它,如下所示:

#include <boost/optional.hpp>      
#include <boost/utility/in_place_factory.hpp>
#include <folly/ApplyTuple.h>

namespace boost                                                                      
{                                                                                  
  template<class ... As> 
  struct rvalue_in_place : public in_place_factory_base 
  {
    mutable std::tuple<As...> tup_; 
    rvalue_in_place(As ... as)
      : tup_(std::forward<As>(as)...)
   {
   }                                                                                   
   template< class T >
   void apply ( void* address ) const 
   {
     auto make = [address](As ... as) { new (address) T(std::forward<As>(as)...);  }; 
     folly::applyTuple(make, tup_); 
   }
 };   
 template <class ... As> 
 rvalue_in_place<As&&...> in_place(As && ... a)
 {
   return rvalue_in_place<As&&...>(std::forward<As>(a)...);
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM