Re: template + lambda function matching - howto
On 2012-01-30 13:57, Helmut Jarausch wrote:
gcc 4.7 (git) fails to compile the following lines
template<typename Element>
void QuickSort( vector<Element>& V, int lo, int hi,
bool less(const Element& A, const Element& B) ) { ... }
and lateron
vector<int> V;
QuickSort(V,0,49, [](const int& A, const int& B)->bool {return A<B;} );
gcc complains:
template argument deduction/substitution failed:
mismatched types 'bool (*)(const Element&, const Element&)' and
'main()::<lambda(const int&, const int&)>'
Is this a bug in gcc or am I missing something.
You are missing that your QuickSort is a function template, which means that it will try to match all arguments for depending parameters exactly. Given that, you do not provide a function pointer with a signature of bool(*)(const int&, const int&), but some compiler-defined class type (the closure type). The compiler will therefore successfully match the first argument to deduce Element==int, but cannot deduce a type value for the function pointer.
What can I do to make it conform to the standard?
You have to disable argument deduction for the second depending parameter by transforming it into a non-deduced context, e.g. like so:
template <typename T>
struct identity { typedef T type; };
template <typename Element>
void QuickSort(std::vector<Element>& V, int lo, int hi,
typename identity<bool(*)(const Element&, const Element&)>::type);
This signature will ensure that parameter "Element" is solely deduced from the first argument.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]