I have a template function with two template parameters.
The first (T
) is deduced based on the type of the first parameter past to the function. The second (ItrT
) is deduced by use of std::type_traits
and T
.
When I use ItrT
as the type for a parameter (see function bar
) than all types are deduced implicitly, but when I use std::function<void(ItrT)>
as the type of a parameter (see function foo
) than the correct types can only be deduced when fully specifying all template parameter(Even if using the exact same code as in the function definition). One would imagine that using ItrT
inside a template wouldn't change the compilers ability to deduce the template.
Why isn't this the case? And what do I need to do, so that all template parameter can be implicitly deduced?
I'm using C++17.
template <class T, class ItrT = typename std::iterator_traits<T>::value_type>
auto foo(T iterator, std::function<void(ItrT)> expr) -> void{
}
template <class T, class ItrT = typename std::iterator_traits<T>::value_type>
auto bar(T iterator, ItrT expr) -> void{
}
int main() {
std::vector<int> vec = {1, 2, 3};
bar(vec.begin(), 1); // Compiles!
foo(vec.begin(), [](int) {}); // Failes!
foo<decltype(vec.begin()),
std::iterator_traits<decltype(vec.begin())>::value_type>
(vec.begin(), [](int) {}); // Compiles!
}