dimanche 4 juillet 2021

What's wrong with lambda function ? Complete understanding of problems with Lambda function?

I am going through online C++ course. Now the tutor has given us few assignment questions, i solved most of them but I am kind of stuck with this one. The question is

1."what is wrong with this code and how will you fix it".

As given in assumption every thing is rightly implement, so I am just assuming that the issue is with lambda function.(please let me know if i am missing anything else. I dont have much experience in lambda)

// Assumptions:
// - this code compiles
// - AsyncClient, GameData and BackendData have proper implementation accessible in this scope
// - functions declared in the Utils namespace are properly implemented as well

namespace Utils
{
    float GetCurrentTimestamp();
    GameData ConvertBackendData(const BackendData& inBackendData);
}

class AsyncDataDepot
{
public:
    AsyncDataDepot(const std::shared_ptr<AsyncClient>& inAsyncClient)
    : asyncClient{inAsyncClient}
    {}

    void RequestAsyncData(GameData& outGameData)
    {
        asyncClient->Request("GET",
            [this, &outGameData](const BackendData& inBackendData)
            {
                lastResponseTimestamp = Utils::GetCurrentTimestamp();
                outGameData = Utils::ConvertBackendData(inBackendData);
            } );
    }
private:
    std::shared_ptr<AsyncClient> asyncClient;
    uint32_t lastResponseTimestamp;
};

Now i am following https://en.cppreference.com/w/cpp/language/lambda , this website to understand what is wrong with lambda but i am not getting anything, is it that "this" is being passed in lambda function.

2. what are the problems with lambda, if any, with capturing variable by copy, capturing variable by reference, capturing defaults (either [=] or [&]) and capturing this pointer.

Sorry for asking 2 question but this will give me and other who ever is reading this post complete idea about the lambda and its problems.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire