mercredi 5 décembre 2018

C++ lambda as templated parameter of a function not working [duplicate]

This question already has an answer here:

I'm trying to pass a lambda into a parameter in the maybe() function, which is working strangely. Code is below:

template<typename R, typename F>
void maybe(R& result, F& lambda) {
    if (0 == result) {  
        result = lambda();
    }
}

auto l = [&]() {
    return adi_uart_Open(deviceNum, ADI_UART_DIR_BIDIRECTION, &memory, ADI_UART_BIDIR_DMA_MEMORY_SIZE, &handle);
};

If I call

maybe(result, l);

then everything works just fine. However, if I put the lamba into the function directly, like:

maybe(result, [&](){return adi_uart_Open(deviceNum, ADI_UART_DIR_BIDIRECTION, &memory, ADI_UART_BIDIR_DMA_MEMORY_SIZE, &handle);});

then I get the following error:

error: no instance of function template "maybe" matches the argument list

I'd use std::function instead of templates, but it's not available on the embedded device i'm working on.

Aucun commentaire:

Enregistrer un commentaire