I have following template function which has template template parameter as its argument.
template<typename T,
template <typename... ELEM> class CONTAINER = std::vector>
void merge(typename CONTAINER<T>::iterator it )
{
std::cout << *it << std::endl;
}
And the following code uses this code.
std::vector<int> vector1{1,2,3};
merge<int>(begin(vector1));
It works as expected, but when I use
merge(begin(vector1));
It cannot deduce type of T
.
I thought that it could deduce type from std::vector<int>::iterator it;
as int
.
Why the compiler can't deduce the type?
CONTAINER
, but using the default.std::set<int> set1 {1, 2, 3}; merge<int>(begin(set1));
fails – Calethint
fromdecltype(it)
, then usestd::iterator_traits<>::value_type
. E.g.template <typename Iterator, typename Value = std::iterator_traits<Iterator>::value_type> void merge(Iterator it)
– Caleth