Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to interpret the lambda function as shown?

The following lines are taken from Autosar Adaptive Davinci source code and I have renamed/obscured parts of some information but kept the format intact.

class interface {
public:
    adaptive::core::Result<void> FunctionABC() {
        adaptive::core::Result<void> result{};
        // Do something
        return result;
    }
};
void main() {
    interfaceABC= std::make_unique<interface>();
    interfaceABC->FunctionABC().InspectError([this](adaptive::core::ErrorCode const& error) {
        logAMessage() << "Starting interface failed:" << error.Message() << ".";
    });
}

Here I am confused with the line about InspectError(). It feels like it is a lambda function starting from [this] but I fully don’t understand on when is InspectError called. Is it after FunctionABC() has returned a result or something else? Notice that it is error message with statement starting interface failed.

P.S. I do not see any definition of InspectError inside FunctionABC.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks.

>Solution :

I am not actually sure what you are asking. InspectError is called in main. It is a member function of the adaptive::core::Result<void> returned from the call to FunctionABC().

You cannot capture this in main because there is no this in main. Also interfaceABC= std::make_unique<interface>(); makes no sense. Moreover main must return int.

Perhaps things get more clear for you when turning the code into something that actually compiles:

#include <memory>
#include <functional>
#include <iostream>

struct adaptive_core_errorcode_mock {
    std::string Message() const { return "hello world";}
};


struct adaptive_core_result_mock { 
    using ftype = std::function<void(const adaptive_core_errorcode_mock&)>;
    ftype f;
    void InspectError(ftype g) { f = g;}

};


class interface {
public:
    adaptive_core_result_mock FunctionABC() {
        return {};
    }
};
int main() {
    auto x = std::make_unique<interface>();
    x->FunctionABC().InspectError([](adaptive_core_errorcode_mock const& error) {
        std::cout  << "Starting interface failed:" << error.Message() << ".";
    });
}

FunctionABC returns a adaptive_core_result_mock which has a method called InspectError. That method takes a callable of certain signature. Starting from here one has to guess. I guess the callable is stored to be called later. In the code above the lambda is never actually called. The adaptive_core_result_mock could call it via f().

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading