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.
maybe
takes an lvalue reference:
maybe(R& result, F& lambda)
C++ forbids a non-const lvalue reference to be bound to a temporary. Add a const.
maybe(R& result, F const& lambda)
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.