Topic: typename necessary? - A Related Question
Author: Jason Merrill <jason@cygnus.com>
Date: 1998/11/30 Raw View
>>>>> Shiva <jshiva@bigfoot.com> writes:
> So you answer also proves wrong somebody else's reply that microsoft did
> a clever thing by making the compiler work without typename. It's less
> difficult to make it work without typename. Am I right ?
Unless you change your implementation strategy. Old implementations
would suck up a template definition as text or preprocessor tokens and feed
it back into the parser when an instantiation was needed. Adding
'typename' to the language allows for an implementation that parses the
templates immediately and stores them in a partially-compiled form.
Jason
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1998/11/25 Raw View
On 25 Nov 98 16:18:08 GMT, Martin Fabian <fabian@NOSPAM.s2.chalmers.se> wrote:
>Shiva wrote:
>> Stroustrup says without the typename keyword, the compiler has no way
>> of knowing if a member of a template argument type is a typename.
>It seems that MSVC++ is "psychic" in this respect, since it doesn't
>require the typename. There's no problem, for instance, having in a
>template class
>
>typedef std::iterator_traits<T>::pointer Pointer;
>
>So the answer is "Stl works without this because the compiler works it
>out". I guess, for once they've been clever over at M$ (perhaps too
>clever :-)
A lot of compilers don't require the 'typename' because since you
are declaring a typedef, the thing to the right of the typedef is
obviously a 'typename'. This actually makes sense to me. In
other contexts (where you aren't making a typedef) the 'typename'
is necessary, and most of these compilers will flag the missing
'typename' as an error. The rule in C++ is therefore that
'typename' is always required.
Similarly, when taking the address of a member function, the '&' is
required, but many compilers let you get away without it. There is
no such thing as member-function to pointer standard conversion.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/11/25 Raw View
Shiva <jshiva@bigfoot.com> writes:
>Stroustrup says without the typename keyword, the compiler has no way
>of knowing if a member of a template argument type is a typename.
> ...
>So, how does Dimkumware's STL work without this ??
>( THIS IS THE QUESTION )
"Typename" is a recent addition. It is not necessary to enable
the compiler to to deal with templates. It IS necessary for a
compiler to be able to report errors in the template definition,
as opposed to waiting until the template is instantiated.
The typename requirement was added to the standard to permit
compilers to diagnose errors in template defintions earlier
than was previously possible. Some compilers haven't been
modified yet to REQUIRE the keyword or to attempt to diagnose
errors in template definitions prior to instantiation.
Example, supposing "typename" were not a requirement:
int i;
template< class T > void f(const T& t)
{
T::foo * i; // #1
char *i; // #2
...
}
Line #1 might invoke a possibly overloaded multiply operation on a
static member foo of class T and global i, or it might declare a local
i to be a pointer to type T::foo.
In the first case, line #2 declares a local i. In the second case,
line #2 is an error.
The compiler cannot know which interpretation is correct, or if
either one is correct. It therefore cannot determine how to parse
later expressions involving 'i'.
The compiler must therefore delay any error reporting until the
template is instantiated. (During instantiation, T and its members
are known.) That in turn means getting many of the same errors on
every instantiation.
The requirement of "typename" means the compiler knows which
names in the template are types. It can therefore do some
parsing of declarations and expressions, and report some kinds
of errors before seeing an instantiation. It could then skip
requested instantiations and avoid duplicate error messages.
In standard C++, line 1 cannot be a declaration, since T::foo
is not declared to be a type. Line 2 therefore declares a local i
as a char*. The compiler doesn't know whether line 1 is valid at
this point, but it can generate errors on any later uses of i
that are not consistent with i being a local char*.
---
Steve Clamage, stephen.clamage@sun.com
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/11/26 Raw View
On Nov 25, 1998, Shiva <jshiva@bigfoot.com> wrote:
> but I checked Dimkumware's STL, the typename is missing.
> typedef _A::size_type size_type;
> typedef _A::difference_type difference_type;
> Stroustrup says without the typename keyword, the compiler has no way
> of knowing if a member of a template argument type is a typename.
> So, how does Dimkumware's STL work without this ??
Maybe it just special-cases `typedef', since after `typedef' it
doesn't make sense to have anything other than a type name.
Nevertheless, in order to be compliant with the C++ Standard, it
should issue a diagnostic.
--
Alexandre Oliva http://www.dcc.unicamp.br/~oliva aoliva@{acm.org}
oliva@{dcc.unicamp.br,gnu.org,egcs.cygnus.com,samba.org}
Universidade Estadual de Campinas, SP, Brasil
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Shiva <jshiva@bigfoot.com>
Date: 1998/11/26 Raw View
Steve Clamage wrote:
>
> Shiva <jshiva@bigfoot.com> writes:
>
> >Stroustrup says without the typename keyword, the compiler has no way
> >of knowing if a member of a template argument type is a typename.
>
Great answer Steve.
Exactly the type of reason I was looking for when I had originally
posted the question comp.lang.c++ & got the invisible type replies & was very dissapointed.
So you answer also proves wrong somebody else's reply that microsoft did a clever thing by making
the compiler
work without typename. It's less difficult to make it work without typename. Am I right ?
--
rgds,
Shiva
comp.lang.c++ FAQ: http://www.cerfnet.com/~mpcline/c++-faq-lite/
http://members.xoom.com/jshiva/
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Shiva <jshiva@bigfoot.com>
Date: 1998/11/25 Raw View
I had posted a related question long back for which I didn't get any
satisfactory replies.(Pete Becker replied saying it's there in the
newer version - which really doesn't answer my Question
PJ Plauger replied saying it's invisible - whatever that means )
So I'll try again.
Here GOES
According to stroustrup 3E,
a vector class should look like this
template<class _Ty, class _A = allocator<_Ty> >
class vector {
public:
typedef typename _A::size_type size_type;
typedef typename _A::difference_type difference_type;
....
};
but I checked Dimkumware's STL, the typename is missing.
typedef _A::size_type size_type;
typedef _A::difference_type difference_type;
Stroustrup says without the typename keyword, the compiler has no way
of knowing if a member of a template argument type is a typename.
So, how does Dimkumware's STL work without this ??
( THIS IS THE QUESTION )
--
rgds,
Shiva
comp.lang.c++ FAQ: http://www.cerfnet.com/~mpcline/c++-faq-lite/
http://members.xoom.com/jshiva/
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Martin Fabian <fabian@NOSPAM.s2.chalmers.se>
Date: 1998/11/25 Raw View
Shiva wrote:
>
> According to stroustrup 3E,
> a vector class should look like this
>
> template<class _Ty, class _A = allocator<_Ty> >
> class vector {
> public:
> typedef typename _A::size_type size_type;
> typedef typename _A::difference_type difference_type;
> ....
> };
>
> but I checked Dimkumware's STL, the typename is missing.
> typedef _A::size_type size_type;
> typedef _A::difference_type difference_type;
>
> Stroustrup says without the typename keyword, the compiler has no way
> of knowing if a member of a template argument type is a typename.
>
> So, how does Dimkumware's STL work without this ??
> ( THIS IS THE QUESTION )
>
It seems that MSVC++ is "psychic" in this respect, since it doesn't
require the typename. There's no problem, for instance, having in a
template class
typedef std::iterator_traits<T>::pointer Pointer;
So the answer is "Stl works without this because the compiler works it
out". I guess, for once they've been clever over at M$ (perhaps too
clever :-)
--
Martin Fabian http://www.s2.chalmers.se/~fabian/
--
"Say hello to my little friend." (Tony Montana)
"Ka-Boom!" (his little friend)
/* Remove NOSPAM from reply-to address to mail me */
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]