typename

  • Thread starter Thread starter Staffan Langin
  • Start date Start date
S

Staffan Langin

Hello,

In the following code-snippet,

template<class Base>

class Foo : public Base

{

typedef typename Base::Type Type; //1

Type FooBar();

};

template<class Base> typename Foo<Base>::Type //2

Foo<Base>::FooBar()

{

return 0;

}

struct Bar {typedef int Type;};

template class Foo<Bar>;



The C++ standard requires the typename at 1 but not at 2. However, the
compiler included in Visual Studio 2005 also requires typename at 2
resulting in a LOT of errors in my current project. Is this a known problem
that is scheduled to be fixed in the release version of VS2005?



Staffan Langin
 
Staffan said:
template<class Base>
class Foo : public Base {
typedef typename Base::Type Type; //1
Type FooBar();
};

template<class Base> typename Foo<Base>::Type //2
Foo<Base>::FooBar() { return 0; }
[...]
The C++ standard requires the typename at 1 but not at 2. However, the
compiler included in Visual Studio 2005 also requires typename at 2
resulting in a LOT of errors in my current project.

I'm not confident that you're correct about (2) being allowed by the
standard without a "typename" keyword. The type Foo<Base>::Type is clearly a
dependent type on the template parameter Base, and section 14.6.3 of the ISO
C++ standard says:

"A qualified-name that refers to a type and that depends on a
template-parameter (14.6.2) shall be prefixed by the keyword typename to
indicate that the qualified-name denotes a type, forming an
elaborated-type-specifier (7.1.5.3)."

Because Foo<Base>::Type is a qualified type name (it uses ::), this seems to
mandate the keyword. Paragraphs 5 and 6 reinforce this:

"The keyword typename shall only be used in template declarations and
definitions, including [...] in the return type for the definition of a
member function of a class template [...]" (this para allows but does not
require its use here)

"Within the definition of a class template or within the definition of a
member of a class template, [...] [t]he keyword typename shall always be
specified when the member is referred to using a qualified name, even if the
qualifier is simply the class template name."

For comparison, this (more or less) equivalent code compiles cleanly,
because it uses an unqualified type name, even though the type Type is still
dependent:

template<class Base>
class Foo : public Base {
typedef typename Base::Type Type;
Type FooBar() {
return 0;
}
};

It's possible that your previous compiler was more lenient, or
misinterpreted the standard; previous versions of Visual Studio did allow
one to omit this keyword (according to the MSDN entry for C4346). There
doesn't seem to be any ambiguity in leaving it out, since you can only have
a type name in that position, but the standard seems to require it, if I'm
not misinterpreting it. If you can justify your statement to the contrary
I'd like to hear your argument. I hope this helps.
 
It's possible that your previous compiler was more lenient, or
misinterpreted the standard; previous versions of Visual Studio did allow
one to omit this keyword (according to the MSDN entry for C4346). There
doesn't seem to be any ambiguity in leaving it out, since you can only
have a type name in that position, but the standard seems to require it,
if I'm not misinterpreting it. If you can justify your statement to the
contrary I'd like to hear your argument. I hope this helps.

Derrick,

If I interpret the defect report,
www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#224, correctly the
name at 2 is a member of the current instantiation and doesn't require the
typename keyword.

Staffan Langin
 
In the following code-snippet,
template<class Base>
class Foo : public Base
{
typedef typename Base::Type Type; //1
Type FooBar();
};

template<class Base> typename Foo<Base>::Type //2
Foo<Base>::FooBar()
{
return 0;
}

struct Bar {typedef int Type;};
template class Foo<Bar>;


The C++ standard requires the typename at 1 but not at 2.

Staffan,

The Comeau online compiler seems to agree.
However, the
compiler included in Visual Studio 2005 also requires typename at 2
resulting in a LOT of errors in my current project. Is this a known problem
that is scheduled to be fixed in the release version of VS2005?

It's no different in the Aug CTP release, the compiler gives these
errors without typename:

warning C4346: 'Foo<Base>::Type' : dependent name is not a type
error C2143: syntax error : missing ';' before 'Foo<Base>::FooBar'
error C4430: missing type specifier - int assumed. Note: C++ does not
support default-int
fatal error C1903: unable to recover from previous error(s); stopping
compilation

I suggest that you report it as a bug here:

http://lab.msdn.microsoft.com/productfeedback/default.aspx

and post a link back here to your bug report and I'll add a
confirmation vote to it.

Dave
 
I see Jonathan Caves has already replied to the report - I've added a
further note to see if he'll clarify this - it'd be nice to know that
he can reproduce it on an earlier build of the compiler, and that it's
now definitely resolved.

If that's the case, I'd be very pleased. BTW, when can one expect the
release version of VS2005?

Staffan
 
If that's the case, I'd be very pleased. BTW, when can one expect the
release version of VS2005?

I think it's been publically stated to be available sometime around
the beginning of Nov.

Dave
 
Staffan said:
If I interpret the defect report,
www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#224, correctly
the name at 2 is a member of the current instantiation and doesn't
require the typename keyword.

I apologise - you're correct. I'm afraid I was misinterpreting this portion
of the standard. I hope the workaround of adding the keyword is sufficient
for your purposes (or that you're willing to wait for VS2005 to release).
Sorry about that.
 
Derrick said:
I apologise - you're correct. I'm afraid I was misinterpreting this
portion of the standard. I hope the workaround of adding the keyword
is sufficient for your purposes (or that you're willing to wait for
VS2005 to release). Sorry about that.

I see what happened now - after reading the item at
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#224 , it seems
that my interpretation was based on the 1998 edition of the C++ standard,
which exhibited this issue. I'll be sure to refer to the most recent version
in the future. Sorry about that.
 
David said:
I see Jonathan Caves has already replied to the report - I've added a
further note to see if he'll clarify this - it'd be nice to know that
he can reproduce it on an earlier build of the compiler, and that it's
now definitely resolved.

I don't think it's fixed. Rather, I think Jonathan probably tried to
compile the code as-is, saw that it compiled, and assumed it was fixed (the
code as-posted has the extra 'typename' already).

I tested it on build 14.00.50727.24, which dates from less than a week ago
and nothing had changed.

I'd suggest re-activating the case on Product Feedback Center - although I'd
be very surprised if they fix it this late in the release cycle.

-cd
 
I don't think it's fixed.

That was my suspicion too.
I tested it on build 14.00.50727.24, which dates from less than a week ago
and nothing had changed.

I've requested it be re-opened.
I'd suggest re-activating the case on Product Feedback Center - although I'd
be very surprised if they fix it this late in the release cycle.

Yeah, they won't even add pragamas to header files to eliminate
warnings that the new static analyser picks up on at this stage. :(

Dave
 
Back
Top