I have a bubble sort function that takes an array, a comparison function, and a boolean that indicates whether to sort the array in reverse order. This is a template function that supports any type of data and automatically displays the size of the array.
When specifying a comparison function, if I pass a pointer to a function, the compiler will automatically select the data type of the array, which is great. But if I pass the lambda instead, it will not be output automatically. I have to specify the data type explicitly, or static_castlambda as fnCompare_t<double>.
What is the reason for this? Because according to this post , as long as the lambda does not capture, it can be used as a pointer to a plain old function, but it seems like this is not always the case? Why can this be different in this case?
#include <iostream>
using namespace std;
template <typename T>
using fnCompare_t = int(*)(T const &, T const &);
template <typename T, size_t count>
inline void BubbleSort(
T(&array)[count],
fnCompare_t<T> fnCompare,
bool reverse)
{
cout << "TODO: Implement BubbleSort" << endl;
}
double doubleArray[] = {
22.3, 11.2, 33.21, 44.2, 91.2, 15.2, 77.1, 8.2
};
int CompareDouble(double const & a, double const & b)
{
return a > b ? 1 : a == b ? 0 : -1;
}
int main()
{
auto fnCompare = [](double const & a, double const & b) -> int {
return a > b ? 1 : a < b ? -1 : 0;
};
BubbleSort(doubleArray, CompareDouble, false);
BubbleSort(doubleArray, static_cast<fnCompare_t<double>>(fnCompare), false);
BubbleSort<double>(doubleArray, fnCompare, false);
return 0;
}
source
share