I am trying to make a template function that is will only get used for invocables that match a certain i pattern (in the example, accept int
and return int
). I tried this but it doesn't compile. Can anyone explain to me why?
#include <iostream>
#include <type_traits>
int foo(int) {return 0;}
int foo(char*) {return 0;}
template<class T>
std::enable_if_t<std::is_invocable_r_v<int, T, int>>
add(T t) {}
int main()
{
add(foo);
}
The error I get (VS2017):
main.cpp(13): error C2672: 'add': no matching overloaded function found
main.cpp(13): error C2783: 'enable_if<_Test,_Ty>::type add(T)': could not deduce template argument for 'T'
with
[
_Ty=void
]
main.cpp(9): note: see declaration of 'add'