简体   繁体   中英

Access to templated lambda from std::function in C++

I'm trying to add some instrumentation to my lambda in order to store some data (ie when the lambda has been triggered) but I'm having some trouble to retrieve a lambda from an std::function. I have created a generic functor in order to store any lambda and my parameters and then I store this functor into an std::function.

When I try to access to my functor from the std::function, the type passed through the target method is not working... The type I passed doesn't seems to be the good one and I don't know how to pass the lambda type which is not a type by definition...

Could anyone help me on how to achieve that ? I'm using Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27030.1 for x64

Thanks

Here is an example:

#include <iostream>
#include <functional>
#include <string>

template <typename Signature>
class lambda_wrapper
{
    public:
    lambda_wrapper(Signature&& lambda, const std::string& iName)
        : _lambda(std::move(lambda))
        , _sName(iName)
    {}

    template <typename... Args> inline
        auto operator()(Args&&... args)
    {
        return _lambda(std::forward<decltype(args)>(args)...);
    }

    std::string& getName()
    {
        return _sName;
    }
    private:
    Signature _lambda;
    std::string _sName;
};

template <typename Signature>
lambda_wrapper<Signature> myLambda(Signature&& lambda, const std::string& iName)
{
    return {std::forward<Signature>(lambda), iName};
}

class MyClass
{
    public:
    MyClass()
        : _lambda(myLambda([](int)
    {}, "test"))
    {

    }
    virtual ~MyClass() = default;

    void printVal()
    {
        lambda_wrapper<std::function<void(int)>>* pWrapper = _lambda.target<lambda_wrapper<std::function<void(int)>>>();
        if (pWrapper)
            std::cout << pWrapper->getName();
        else
            std::cout << "NULL" << std::endl;

        std::cout << "Stored type name=" << _lambda.target_type().name() << std::endl;
        std::cout << "Cast type name=" << typeid(lambda_wrapper<std::function<void(int)>>).name() << std::endl;
    }

    private:
    std::function<void(int)> _lambda;
};

int main()
{
    MyClass myClass;
    myClass.printVal();
}

and here is the result:

NULL
Stored type name=class lambda_wrapper<class <lambda_de654b4bbc7b5fec872744c55537b509> >
Cast type name=class lambda_wrapper<class std::function<void __cdecl(int)> >

The correct type for _lambda.target isn't nameable in your example. Each lambda expression creates an object with a unique type.

Rather than using std::function and trying to get a lambda back out, you could extend lambda_wrapper to type erase like std::function . Then you can easily add in behaviour to the part that knows the actual type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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