Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is typename _not_ needed here in Visual Studio 2008/2010?

In this question, the asker has the following function:

template<typename ITER>
bool nextPermutation(ITER start, ITER end)
{
    return nextPermutation(start, end, std::iterator_traits<ITER>::iterator_category());
}

Why isn't a typename needed before the std::iterator_traits? I thought it was needed for nested types of a template, if the template is dependent on a template parameter itself? GCC seems to support my idea, as it doesn't compile under both 4.3.4 and 4.5.1, demanding a typename. Even so, it still compiles just fine under both Visual Studio 2008 and 2010.
Is this just another Visual Studio extension/bug I don't know about?
Or is it actually possible to deduce that iterator_category is either a type or a function because it's followed by a pair of parenthesis ()? (See @DeadGM's messages starting here.) So is this maybe actually a bug in GCC?

like image 894
Xeo Avatar asked Apr 15 '11 23:04

Xeo


2 Answers

Visual C++ is well-known not to (fully) support two-phase lookup, which is the underlying reason for why typename is required in the first place. If the compiler doesn't fully support this, it might not fully parse a template before it is instantiated, by which time it "knows" that std::iterator_traits<ITER>::iterator_category is a type. Obviously, this deficiency extends to VC10.

When it comes to typename, I'd trust GCC over VC any day.

like image 193
sbi Avatar answered Oct 16 '22 21:10

sbi


Doesn't MSVC implement the late-parsing scheme? In such a scheme, the compiler isn't dependent on typename. It just stores all the token in between the template definition's braces, and when the template is instantiated, it parses those tokens. Since it then knows what is and what is not a type, it will work without typename.

But if the compiler doesn't diagnose the missing typename when you instantiate the template, then it's non-conforming.

Or is it actually possible to deduce that iterator_category is either a type or a function because it's followed by a pair of parenthesis ()?

All that matters is whether the name is dependent and qualified. Whether or not the template could deduce itself that the name is always a type doesn't matter. It might matter for the quality of error messages for missing typenames though.

FWIW, no it's not possible to deduce anything about iterator_category on a language level.

like image 6
Johannes Schaub - litb Avatar answered Oct 16 '22 20:10

Johannes Schaub - litb