相关疑难解决方法(0)

如何将unique_ptr捕获到lambda表达式中?

我尝试过以下方法:

std::function<void ()> getAction(std::unique_ptr<MyClass> &&psomething){
    //The caller given ownership of psomething
    return [psomething](){ 
        psomething->do_some_thing();
        //psomething is expected to be released after this point
    };
}
Run Code Online (Sandbox Code Playgroud)

但它没有编译.有任何想法吗?

更新:

AS建议,需要一些新的语法来明确指定我们需要将所有权转移到lambda,我现在考虑以下语法:

std::function<void ()> getAction(std::unique_ptr<MyClass> psomething){
    //The caller given ownership of psomething
    return [auto psomething=move(psomething)](){ 
        psomething->do_some_thing();
        //psomething is expected to be released after this point
    };
}
Run Code Online (Sandbox Code Playgroud)

它会是一个好的候选人吗?

更新1:

我将展示我的实施movecopy如下:

template<typename T>
T copy(const T &t) {
    return t;
}

//process lvalue references
template<typename T>
T move(T &t) {
    return …
Run Code Online (Sandbox Code Playgroud)

c++ lambda unique-ptr c++11

38
推荐指数
4
解决办法
3万
查看次数

通过std :: bind传递rvalues

我想将一个rvalue传递std::bind给一个在C++ 0x中采用rvalue引用的函数.我无法弄清楚该怎么做.例如:

#include <utility>
#include <functional>

template<class Type>
void foo(Type &&value)
{
    Type new_object = std::forward<Type>(value);    // move-construct if possible
}

class Movable
{
public:
    Movable(Movable &&) = default;
    Movable &operator=(Movable &&) = default;
};

int main()
{
    auto f = std::bind(foo<Movable>, Movable());
    f();    // error, but want the same effect as foo(Movable())
}
Run Code Online (Sandbox Code Playgroud)

c++ std rvalue rvalue-reference c++11

27
推荐指数
1
解决办法
1万
查看次数

std :: bind是否一般只使用move-only类型,特别是std :: unique_ptr?

我正在尝试使用boost :: asio并遇到一些泥潭.

我正在尝试编译以下代码:

std::unique_ptr<buffer_t> buffer = buffers.pop();
std::function<void(const boost::system::error_code&, size_t)> t = std::bind(&tcp_client::handle_read_done,
                                                                            this,
                                                                            std::placeholders::_1,
                                                                            std::placeholders::_2,
                                                                            std::move(buffer));
Run Code Online (Sandbox Code Playgroud)

如果我排除std :: move(缓冲区),当然从handle_read_done的签名和std :: bind中的传递参数,一切正常.

当试图将它传递给boost :: asio :: async_read_some时,它抱怨从std :: bind返回的对象上隐式删除的函数,对元组上的已删除函数,我假设是因为可移动性,以及大量的提升具体错误.如果我只是尝试将它分配到一个std :: function,它应该与提升调用的签名匹配,我得到那些相同的元组错误,所以我猜它们是相同的.只需将std :: bind的结果赋值给auto就不会产生编译错误,但当然我不能在其上调用任何东西.

我究竟做错了什么?以下是尝试分配时的输出std::function<void(const boost::system::error_code&,size_t)>

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/functional: In static member function ‘static void std::_Function_handler<void(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Functor = std::_Bind<std::_Mem_fn<void (rcon::net::tcp_client::*)(const boost::system::error_code&, long unsigned int, std::unique_ptr<std::array<unsigned char, 10240ul> >)>(rcon::net::tcp_client*, std::_Placeholder<1>, std::_Placeholder<2>, std::unique_ptr<std::array<unsigned char, 10240ul> >)>, _ArgTypes = {const boost::system::error_code&, long unsigned int}]’:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/functional:2148:6:   instantiated from ‘std::function<_Res(_ArgTypes …
Run Code Online (Sandbox Code Playgroud)

c++ bind unique-ptr c++11

11
推荐指数
1
解决办法
5325
查看次数

标签 统计

c++ ×3

c++11 ×3

unique-ptr ×2

bind ×1

lambda ×1

rvalue ×1

rvalue-reference ×1

std ×1