2
template<class mapT, class K, class V>
void f(mapT& m, const K& k, const V& v)
{
    pair<mapT::iterator, bool> p = m.insert(make_pair(k, v));
}

MSVC accepts this code with no errors or warnings. What does the standard have to say about this? Are we allowed to (optional), not allowed to (forbidden), or required to (mandatory) qualify T::iterator with typename in the example above? I am particularly interested in C++03 rules although if anything has changed for 11 it would be nice to know. Thank you.

Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • Does MSVC also accept it for function templates? I ask because for a function, it's possible that there are multiple overloads, where some have a type template argument, and others a non-type template argument. –  Jun 11 '14 at 09:14
  • @hvd: AFAIK MSCV never requires the typename keyword as it does not really do a two phase lookup. It does the lookup upon instantiation – Armen Tsirunyan Jun 11 '14 at 09:18
  • Sure, but the reason I asked is that in class templates, the context definitely requires a type name, so loosening any restriction on a `typename` keyword cannot affect valid code. For function templates, it can affect valid code. –  Jun 11 '14 at 09:45
  • IIRC, MSVC also does not require the `.template` disambiguation in all instances – TemplateRex Jun 11 '14 at 10:35

1 Answers1

3

MSVC is not conformant, the snippet is ill-formed; we must explicitly write typename T::iterator to refer to a type-name iterator inside T, since it's a dependent-type.

This is a known bug in the compiler, see the relevant bug report:


What does the standard say? (14882-2003)

14.6.2.2p1 Dependent types [temp.dep.type]

A type is dependent if it is

  • a template parameter,

  • a qualified-id with a nested-name-specifier which contains a class-names that names a dependent type or whose unqualified-id names a dependent type,

  • ...

14.6.2.4p1 Dependent template arguments [temp.dep.temp]

A type template-argument is dependent if the type it specifies is dependent.

Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196