开发者

Why can't my c++ lambda function be captured?

开发者 https://www.devze.com 2023-03-03 01:19 出处:网络
Say I have a templated Action template <class ArgT> struct Action { Action(::boost::function< void(ArgT) > func)

Say I have a templated Action

template <class ArgT>
struct Action
{
    Action(::boost::function< void(ArgT) > func)
        : func_(func)
    {
    }

    void operator()(ArgT开发者_开发问答 arg)
    {
        func_(arg);
    }

private:
        ::boost::function< void(ArgT) > func_;
};

I use Action like so :

class XCallbackInvoker : public CallbackInvoker< X >
{
public:
    XCallbackInvoker (Action< int > callback)
        : CallbackInvoker< X >(
            Action< ::boost::shared_ptr< X > >(
               [&callback](::boost::shared_ptr< X > x) -> void
               {
                   Action< int > callbackCopy = callback;
                   callbackCopy(x->errorCode());
               }))
    {
    }
};

Edit: CallbackInvoker added

template <class T>
class CallbackInvoker : public ICallbackInvoker
{
public:
    CallbackInvoker(Action< ::boost::shared_ptr< T > > callback)
        : callback_(callback)
    {
    }

    void invoke(::boost::shared_ptr< IBase > message)
    {
        callback_(::boost::static_pointer_cast< T >(message));
    }

private:
    Action< ::boost::shared_ptr< T > > callback_;
};

Now if I don't use a temporary to copy the value referenced by callback, it compiles fine but I get a runtime error (my callback is lost). If I pass my lambda the callback argument by value (that is [=callback]) and don't use the temporary, I get a compile error (my expression would lose some const-volatile qualifiers...)

Why can't I capture my lambda function by value instead of using a temporary?


If you capture by copy then you cannot modify it by default, since the operator() of the lambda is declared const. You need to add mutable to your lambda to allow modifications of the captured variables:

XCallbackInvoker (Action< int > callback)
    : CallbackInvoker< X >(
        Action< ::boost::shared_ptr< X > >(
           [callback](::boost::shared_ptr< X > x) mutable -> void
           {
               callback(x->errorCode());
           }))
{
}
0

精彩评论

暂无评论...
验证码 换一张
取 消