4
votes

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
suggestion: search for "non-deduced context"max66
This question was asked like 3 days ago (and answered): stackoverflow.com/questions/56626007/… .Radosław Cybulski
He is trying exactly the same thing - trying to deduce type T based by T::iterator type. Which is impossible, both in functions and classes.Radosław Cybulski
Note that you also aren't deducing CONTAINER, but using the default. std::set<int> set1 {1, 2, 3}; merge<int>(begin(set1)); failsCaleth
If you only need to get int from decltype(it), then use std::iterator_traits<>::value_type. E.g. template <typename Iterator, typename Value = std::iterator_traits<Iterator>::value_type> void merge(Iterator it)Caleth

1 Answers

6
votes

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);