Consider this code:
#include <functional>
template <typename T,typename COMP>
bool foo(T a,T b,COMP c = std::less<T>()) {
return c(a,b);
}
bool bar(int a, int b){ return a<b;}
int main(){
foo(1,2,bar); // OK
foo(1,2,std::less<int>()); // OK
foo(1,2); // error
}
The first two calls are fine, but it seems to be forbidden to let the compiler infer the type of COMP
from the default parameter:
<source>:14:5: error: no matching function for call to 'foo'
foo(1,2);
^~~
<source>:4:6: note: candidate template ignored: couldn't infer template argument 'COMP'
bool foo(T a,T b,COMP c = std::less<T>()) {
^
1 error generated.
Compiler returned: 1
Am I missing something? I dont really understand why the compiler "couldn't infer template argument 'COMP'" and I rather suspect that it is not allowed to do so.
Is it possible to infer the template argument from a default parameter? If not, why?
>>
operator? – Dynah