C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

rationale behind typename

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
sashan
Guest





PostPosted: Sat Jul 15, 2006 7:19 am    Post subject: rationale behind typename Reply with quote



Hi

I'm wondering why the std specifies that typename be used in cases like
this:

typename Foo<T>::bar b2;

When bar is a type as opposed to a variable? Why can't the compiler
make that decision?

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Peter C. Chapin
Guest





PostPosted: Sat Jul 15, 2006 1:56 pm    Post subject: Re: rationale behind typename Reply with quote



sashan wrote:

Quote:
I'm wondering why the std specifies that typename be used in caseslike
this:

typename Foo<T>::bar b2;

When bar is a type as opposed to a variable? Why can't the compiler
make that decision?

The typename is only needed inside the definition of a template. When
the compiler processes the template it doesn't know what type is
represented by T (that happens later during instantiation). Consequently
it can't tell of bar is a type or a an object. Consider:

template< typename T >
struct Foo {
typedef int bar;
};

// Specialized version for int.
template<>
struct Foo<int> {
static int bar;
};

In the general template the member bar is a type. However, there is a
specialization for which the member bar is an object. Now consider
Foo<T>::bar. If T is int this refers to an object, otherwise it refers
to a type. When the compiler processes the template that contains
Foo<T>::bar it doesn't know (yet) what it is dealing with and so you
must explicitly tell it. If you include typename, Foo<T>::bar is a type,
otherwise it is an object.

Be aware that in general C++ (and C) can't be parsed without knowing
what identifiers are the names of types. This business with typename
wasn't necessary in old C++ because in old C++ template bodies were not
fully parsed as they were read. They were only parsed at instantiation
time when the nature of the template arguments was known. In modern C++
we have two phase lookup. Non-dependent names in the template bodies are
bound to declarations at the time the template itself is seen. To do
this, the template must be fully parsed before the arguments are known
and this leads to the problem that typename solves.

Hope this helps!

Peter

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
James Dennett
Guest





PostPosted: Sat Jul 15, 2006 5:42 pm    Post subject: Re: rationale behind typename Reply with quote



sashan wrote:
Quote:
Hi

I'm wondering why the std specifies that typename be used in cases like
this:

typename Foo<T>::bar b2;

When bar is a type as opposed to a variable? Why can't the compiler
make that decision?

Because, assuming T is a dependent type (i.e., a type depending
on a template parameter), it's unknowable at the time the template
is parsed. There may be some T for which Foo<T>::bar is a type,
and others for which it is an integral constant expression (and
yet others for which it does not exist, though that's not so
relevant here).

In order to allow templates to be checked for syntax, the
compiler must know for each identifier in the template whether
that identifier refers to a type and whether it refers to a
template. This is why the keywords typename and template are
sometimes needed in generic code, when they are not required
in non-generic code.

-- James

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Gabriel Dos Reis
Guest





PostPosted: Mon Jul 17, 2006 6:58 am    Post subject: Re: rationale behind typename Reply with quote

On Sat, 15 Jul 2006, sashan wrote:

| Hi
|
| I'm wondering why the std specifies that typename be used in cases like
| this:
|
| typename Foo<T>::bar b2;

unfortunate limitation of parsing technology that shrines in the
language specification.

| When bar is a type as opposed to a variable? Why can't the compiler
| make that decision?

Consider the following slightly modified case

Foo<T>::bar * foo;

declaration of pointer "foo" or multiplication?

--
Gabriel Dos Reis
gdr (AT) cs (DOT) tamu.edu
Texas A&M University -- Department of Computer Science
301, Bright Building -- College Station, TX 77843-3112

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Gabriel Dos Reis
Guest





PostPosted: Mon Jul 17, 2006 7:33 am    Post subject: Re: rationale behind typename Reply with quote

On Sat, 15 Jul 2006, Peter C. Chapin wrote:

| sashan wrote:
|
| > I'm wondering why the std specifies that typename be used in caseslike
| > this:
| >
| > typename Foo<T>::bar b2;
| >
| > When bar is a type as opposed to a variable? Why can't the compiler
| > make that decision?
|
| The typename is only needed inside the definition of a template. When
| the compiler processes the template it doesn't know what type is
| represented by T (that happens later during instantiation). Consequently
| it can't tell of bar is a type or a an object. Consider:
|
| template< typename T >
| struct Foo {
| typedef int bar;
| };
|
| // Specialized version for int.
| template<>
| struct Foo<int> {
| static int bar;
| };


Yes, but that does not explain why "typename" is needed at the
*parsing time*. The declaration

typename Foo<T>::bar foo;

is invalid for instantiation of Foo with T=int -- assuming your
specializations.
Now, assume the committee decided that Foo<T>::bar -- without the
preceding "typename" -- shall be interpreted as a typename. The above
program with behave the same as if the "typename" was there.

However, not requiring the "typename" means that there is no way to
say

F<T>::B * f;

is actually a multiplication, or that

F<T>::f(x);

is a function call -- remember, C and thereofre C++, allows redundant
parenthesis in declarations.

--
Gabriel Dos Reis
gdr (AT) cs (DOT) tamu.edu
Texas A&M University -- Department of Computer Science
301, Bright Building -- College Station, TX 77843-3112

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Gabriel Dos Reis
Guest





PostPosted: Mon Jul 17, 2006 9:10 am    Post subject: Re: rationale behind typename Reply with quote

On Sat, 15 Jul 2006, James Dennett wrote:

| sashan wrote:
| > Hi
| >
| > I'm wondering why the std specifies that typename be used in cases like
| > this:
| >
| > typename Foo<T>::bar b2;
| >
| > When bar is a type as opposed to a variable? Why can't the compiler
| > make that decision?
|
| Because, assuming T is a dependent type (i.e., a type depending
| on a template parameter), it's unknowable at the time the template
| is parsed. There may be some T for which Foo<T>::bar is a type,
| and others for which it is an integral constant expression (and
| yet others for which it does not exist, though that's not so
| relevant here).

but even in those disparate cases, using "typename" does nto make the
resulting program valid.

Consider

Foo<T>::bar * foo;

or

Foo<T>::bar(x);

Irrespective of whether the instantiations of Foo<T> may behave as you
said, the parser --before going into instantiation considerations--
still needs clues for deciding whether to parse them as declarations
or expressions-statements.

--
Gabriel Dos Reis
gdr (AT) cs (DOT) tamu.edu
Texas A&M University -- Department of Computer Science
301, Bright Building -- College Station, TX 77843-3112

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Gabriel Dos Reis
Guest





PostPosted: Mon Jul 17, 2006 10:16 pm    Post subject: Re: rationale behind typename Reply with quote

On Mon, 17 Jul 2006, James Dennett wrote:

| Gabriel Dos Reis wrote:
| > On Sat, 15 Jul 2006, James Dennett wrote:
| >
| > | sashan wrote:
| > | > Hi
| > | >
| > | > I'm wondering why the std specifies that typename be used in cases like
| > | > this:
| > | >
| > | > typename Foo<T>::bar b2;
| > | >
| > | > When bar is a type as opposed to a variable? Why can't the compiler
| > | > make that decision?
| > |
| > | Because, assuming T is a dependent type (i.e., a type depending
| > | on a template parameter), it's unknowable at the time the template
| > | is parsed. There may be some T for which Foo<T>::bar is a type,
| > | and others for which it is an integral constant expression (and
| > | yet others for which it does not exist, though that's not so
| > | relevant here).
| >
| > but even in those disparate cases, using "typename" does nto make the
| > resulting program valid.
|
| I'm afraid I can't understand that; typename is in the language
| precisely because it makes the program valid, so long as the
| template is used only for suitable types, and because it allows
| the template to be parsed without additional knowledge of the
| types over which it will be instantiated.

My message was saying that putting "typename" in front of the code
does not make it legal if you instantiate Foo with T=int. That you
can't dispute. I was clarifying that "typename" has no bearing to
instantiation as several messages tried to imply. It is a pure
parsing problem, not instantiation problem.

| You snipped the paragraph I wrote explaining that in more
| detail:
|
| In order to allow templates to be checked for syntax,
| the compiler must know for each identifier in the
| template whether that identifier refers to a type and
| whether it refers to a template. This is why the
| keywords typename and template are sometimes needed
| in generic code, when they are not required in
| non-generic code.

This is alsmot correct; but the earlier instantiation stuff I find
confusing Smile
In fact, it is not really checking for syntax as opposed to letting
the compiler proceed in the parsing. The compiler checks almost
nothing in

Foo<T>::bar(x);

or

typename Foo<T>::bar(x);

It trusts the programmer.

| > Consider
| >
| > Foo<T>::bar * foo;
| >
| > or
| >
| > Foo<T>::bar(x);
| >
| > Irrespective of whether the instantiations of Foo<T> may behave as you
| > said, the parser --before going into instantiation considerations--
| > still needs clues for deciding whether to parse them as declarations
| > or expressions-statements.
|
| Yes, which is why typename is used to distinguish which case
| your template intends to support. Without typename, bar is
| not a type name in the above, so we have multiplication or
| a function call (expression statements), but with typename
| they become declarations (with redundant () in the latter).

I know all that. The question was "what if it wasn't required" :-)

--
Gabriel Dos Reis
gdr (AT) cs (DOT) tamu.edu
Texas A&M University -- Department of Computer Science
301, Bright Building -- College Station, TX 77843-3112

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Gennaro Prota
Guest





PostPosted: Mon Jul 17, 2006 10:44 pm    Post subject: Re: rationale behind typename Reply with quote

On Mon, 17 Jul 2006 00:58:47 CST, Gabriel Dos Reis <gdr (AT) cs (DOT) tamu.edu>
wrote:

Quote:
On Sat, 15 Jul 2006, sashan wrote:

| Hi
|
| I'm wondering why the std specifies that typename be used in cases like
| this:
|
| typename Foo<T>::bar b2;

unfortunate limitation of parsing technology that shrines in the
language specification.

So it might be made optional in such cases, in the future? :)

--
Gennaro Prota

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
James Dennett
Guest





PostPosted: Tue Jul 18, 2006 12:35 am    Post subject: Re: rationale behind typename Reply with quote

Gabriel Dos Reis wrote:
Quote:
On Sat, 15 Jul 2006, James Dennett wrote:

| sashan wrote:
| > Hi
|
| > I'm wondering why the std specifies that typename be used in cases like
| > this:
|
| > typename Foo<T>::bar b2;
|
| > When bar is a type as opposed to a variable? Why can't the compiler
| > make that decision?
|
| Because, assuming T is a dependent type (i.e., a type depending
| on a template parameter), it's unknowable at the time the template
| is parsed. There may be some T for which Foo<T>::bar is a type,
| and others for which it is an integral constant expression (and
| yet others for which it does not exist, though that's not so
| relevant here).

but even in those disparate cases, using "typename" does nto make the
resulting program valid.

I'm afraid I can't understand that; typename is in the language
precisely because it makes the program valid, so long as the
template is used only for suitable types, and because it allows
the template to be parsed without additional knowledge of the
types over which it will be instantiated.

You snipped the paragraph I wrote explaining that in more
detail:

In order to allow templates to be checked for syntax,
the compiler must know for each identifier in the
template whether that identifier refers to a type and
whether it refers to a template. This is why the
keywords typename and template are sometimes needed
in generic code, when they are not required in
non-generic code.

Quote:
Consider

Foo<T>::bar * foo;

or

Foo<T>::bar(x);

Irrespective of whether the instantiations of Foo<T> may behave as you
said, the parser --before going into instantiation considerations--
still needs clues for deciding whether to parse them as declarations
or expressions-statements.

Yes, which is why typename is used to distinguish which case
your template intends to support. Without typename, bar is
not a type name in the above, so we have multiplication or
a function call (expression statements), but with typename
they become declarations (with redundant () in the latter).

-- James

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Gabriel Dos Reis
Guest





PostPosted: Tue Jul 18, 2006 1:43 am    Post subject: Re: rationale behind typename Reply with quote

On Mon, 17 Jul 2006, Gennaro Prota wrote:

| On Mon, 17 Jul 2006 00:58:47 CST, Gabriel Dos Reis <gdr (AT) cs (DOT) tamu.edu>
| wrote:
|
| >On Sat, 15 Jul 2006, sashan wrote:
| >
| >| Hi
| >|
| >| I'm wondering why the std specifies that typename be used in cases like
| >| this:
| >|
| >| typename Foo<T>::bar b2;
| >
| >unfortunate limitation of parsing technology that shrines in the
| >language specification.
|
| So it might be made optional in such cases, in the future? :)

I see your smiley; however I must add that I don't see how making it
optional will decrease the confusion around "typename".

--
Gabriel Dos Reis
gdr (AT) cs (DOT) tamu.edu
Texas A&M University -- Department of Computer Science
301, Bright Building -- College Station, TX 77843-3112

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.