By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,417 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,417 IT Pros & Developers. It's quick & easy.

typename

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Staffan Langin wrote:
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.
--
Derrick Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights.
Nov 17 '05 #2

P: n/a
> 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,
http://www.open-std.org/jtc1/sc22/wg...fects.html#224, correctly the
name at 2 is a member of the current instantiation and doesn't require the
typename keyword.

Staffan Langin
Nov 17 '05 #3

P: n/a
>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/produc...k/default.aspx

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

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #4

P: n/a
> I suggest that you report it as a bug here:

http://lab.msdn.microsoft.com/produc...k/default.aspx

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


David,

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

http://lab.msdn.microsoft.com/Produc...a-902187eae8b1

Staffan Langin
Nov 17 '05 #5

P: n/a
>Thanks for your help. I've submitted a bug report now,

http://lab.msdn.microsoft.com/Produc...a-902187eae8b1

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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #6

P: n/a
> 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
Nov 17 '05 #7

P: n/a
>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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #8

P: n/a
Staffan Langin wrote:
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.


If I interpret the defect report,
http://www.open-std.org/jtc1/sc22/wg...fects.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 Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights.
Nov 17 '05 #9

P: n/a
Derrick Coetzee [MSFT] wrote:
Staffan Langin wrote:
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.


If I interpret the defect report,
http://www.open-std.org/jtc1/sc22/wg...fects.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.


I see what happened now - after reading the item at
http://www.open-std.org/jtc1/sc22/wg...fects.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.
Nov 17 '05 #10

P: n/a
Staffan Langin wrote:
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?


Nov 7, 2005

-cd
Nov 17 '05 #11

P: n/a
David Lowndes wrote:
Thanks for your help. I've submitted a bug report now,

http://lab.msdn.microsoft.com/Produc...a-902187eae8b1

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
Nov 17 '05 #12

P: n/a
>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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #13

P: n/a
> I see what happened now - after reading the item at
http://www.open-std.org/jtc1/sc22/wg...fects.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.


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

Staffan Langin
Nov 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.