Помогите найти ошибку в использовании лямбды в сочетании с `std::function`
Использую лямбду в простом вроде бы коде:
#include <iostream>
#include <functional>
using my_type = unsigned int;
int main()
{
my_type def_val = -1;
auto correct_value = [&] (my_type &x, const my_type y, std::function<const my_type& (const my_type&, const my_type&)> selector)
{
if (x != def_val)
x = selector (x, y);
else
x = y;
};
my_type x = 10;
correct_value (x, 20, std::min<my_type>);
std::cout << x << std::endl;
return 0;
}
Но выдаёт ошибку компиляции:
main.cpp:18:5: error: no matching function for call to object of type '(lambda at main.cpp:10:22)'
correct_value (x, 20, std::min<my_type>);
^~~~~~~~~~~~~
main.cpp:10:22: note: candidate function not viable: no overload of 'min' matching 'std::function<const my_type &(const my_type &, const my_type &)>' (aka 'function<const unsigned int &(const unsigned int &, const unsigned int &)>') for 3rd argument
auto correct_value = [&] (my_type &x, const my_type y, std::function<const my_type& (const my_type&, const my_type&)> selector)
Что я сделал не так? Вроде бы 3й аргумент (функция) по прототипу соответствует. Но он всё равно ругается.
Код в онлайн-компиляторе: тык
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Точно укажите компилятору, какую именно функцию вы имеете в виду:
correct_value(x, 20,
static_cast<const my_type & (*)(const my_type &, const my_type &)>(std::min<my_type>));
Код в онлайн-компиляторе: тык.