I was reading about the usage of typename
in C++ template programming (e.g. this Q/A). To me, it seems that when using a dependent nested type name, we should use typename
for avoiding parsing ambiguity. I also checked this on Scot Meyers book effective C++, item #42.
But what is strange for me is that the same example in the book, works without the typename
. Here is the code:
template<class C>
void Print2nd(const C & cont)
{
if (cont.size() >= 2)
{
C::const_iterator * iter1 = new C::const_iterator(cont.begin()); // why typename is NOT needed?
C::const_iterator iter2 = cont.begin(); // why typename is NOT needed?
(*iter1)++;
iter2++;
int value1 = **iter1;
int value2 = *iter2;
std::cout << "The value of 2nd with pointer is: " << value1 << std::endl;
std::cout << "The value of 2nd without pointer is: " << value2 << std::endl;
}
}
int main()
{
std::vector<int> vect = {1,2,3,4,5,6};
Print2nd(vect);
return 0;
}
I am using VS2015. So, the Q is that why typename is not needed in this context? Is there any upgrade in recent C++ compilers to avoid using typename
in such a context? Or I am doing a mistake in the code?
Aucun commentaire:
Enregistrer un commentaire