Groups | Blog | Home
all groups > visual c > august 2005 >

visual c : typename


Staffan Langin
8/29/2005 12:00:00 AM
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

Derrick Coetzee [MSFT]
8/30/2005 10:59:24 AM
[quoted text, click to view]

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.
--
Derrick Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights.

Staffan Langin
8/31/2005 9:06:57 AM
[quoted text, click to view]

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

David Lowndes
8/31/2005 9:43:48 AM
[quoted text, click to view]

Staffan,

The Comeau online compiler seems to agree.

[quoted text, click to view]

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
--
Staffan Langin
9/1/2005 7:27:09 PM
[quoted text, click to view]

David,

Thanks for your help. I've submitted a bug report now,

http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=46f71a7e-0675-4223-928a-902187eae8b1

Staffan Langin

David Lowndes
9/1/2005 9:39:29 PM
[quoted text, click to view]


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.

Dave
--
David Lowndes
9/2/2005 9:11:11 AM
[quoted text, click to view]

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

Dave
--
Staffan Langin
9/2/2005 9:50:59 AM
[quoted text, click to view]

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

Staffan

Derrick Coetzee [MSFT]
9/6/2005 11:26:59 AM
[quoted text, click to view]

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 Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights.

Derrick Coetzee [MSFT]
9/6/2005 11:35:09 AM
[quoted text, click to view]

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.
--
Derrick Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights.

Carl Daniel [VC++ MVP]
9/6/2005 12:04:21 PM
[quoted text, click to view]

Nov 7, 2005

-cd

Carl Daniel [VC++ MVP]
9/6/2005 12:16:27 PM
[quoted text, click to view]

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

David Lowndes
9/6/2005 11:54:31 PM
[quoted text, click to view]

That was my suspicion too.

[quoted text, click to view]

I've requested it be re-opened.

[quoted text, click to view]

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
--
Staffan Langin
9/7/2005 11:21:21 AM
[quoted text, click to view]

No prb. Actually, the defect report hasn't got TC1-status yet so formally
you are correct :-).

Staffan Langin

AddThis Social Bookmark Button