相关疑难解决方法(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万
查看次数

如何表示和传递C++ 11 lambdas?

在c ++ 11中传递lambda非常简单:

func( []( int arg ) {
  // code
} ) ;
Run Code Online (Sandbox Code Playgroud)

但我想知道,将lambda传递给像这样的函数的成本是多少?如果func将lambda传递给其他函数怎么办?

void func( function< void (int arg) > f ) {
  doSomethingElse( f ) ;
}
Run Code Online (Sandbox Code Playgroud)

lambda的传递是否昂贵?由于可以function对象分配0,

function< void (int arg) > f = 0 ; // 0 means "not init" 
Run Code Online (Sandbox Code Playgroud)

它让我认为函数对象就像指针一样.但是,如果不使用new,则意味着它们可能类似于值类型struct或类,它们默认为堆栈分配和成员方式副本.

当你按"值"传递一个函数对象时,C++ 11"代码体"和捕获的变量组是如何传递的?是否有很多代码体的多余副本?我是否必须标记function传递的每个对象,const&以便不进行复制:

void func( const function< void (int arg) >& f ) {
}
Run Code Online (Sandbox Code Playgroud)

或者以某种方式使函数对象以不同于常规C++结构的方式传递?

c++ lambda parameter-passing c++11 std-function

30
推荐指数
1
解决办法
2830
查看次数

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

lambda ×2

unique-ptr ×2

bind ×1

parameter-passing ×1

std-function ×1