Why parameter deduction doesn't work in this template template parameter

Viewed 95

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?

1 Answers

I thought that it could deduce type from std::vector<int>::iterator it; as int.

Why the compiler can't deduce the type?

No.

The compiler can't: look for "non-deduced context" for more information.

And isn't reasonable expecting a deduction.

Suppose a class as follows

template <typename T>
struct foo
 { using type = int; };

where the type type is always int; whatever is the T type.

And suppose a function as follows

template <typename T>
void bar (typename foo<T>::type i)
 { }

that receive a int value (typename foo<T>::type is always int).

Which T type should be deduced from the following call ?

bar(0);
Related