Topic: typename necessary?
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/12/14 Raw View
On Nov 24, 1998, sbnaran@localhost.localdomain.COM (Siemel Naran) wrote:
> //But is the 'typename' keyword ok to use even if it is not required?
Not always. From [temp.res]:
5 The keyword typename shall only be used in template declarations and
definitions, [...] The keyword typename shall only be applied to
qualified names, but those names need not be dependent. [...]
--
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: Chris Kuan <look@sig.please>
Date: 1998/11/25 Raw View
Siemel Naran wrote:
> #include <vector>
> //We know that typename is required when we have templates. Eg,
> template <class Container>
> void f(Container const & c)
> {
> typedef Container::value_type value_type; // error
> typedef typename Container::value_type value_type; // ok
> }
> //But is the 'typename' keyword ok to use even if it is not required?
> void g(std::vector<int> const & c)
> {
> typedef std::vector<int> Container;
> typedef Container::value_type value_type; // ok
> typedef typename Container::value_type value_type; // is this ok?
> }
> Egcs doesn't mind the 'typename' in LINE3 of function g(...)
> It correctly complains about the missing 'typename' in LINE1 of f(...)
> typename.c: In function `void f(const Container &)':
> typename.c:8: `Container::value_type' is not a valid declarator
> typename.c:8: perhaps you want `typename Container::value_type' to make
> it a type
> typename.c:8: parse error before `;'
> */
> /*
> Como doesn't complain about the missing 'typename' in LINE1 of f(...)
> It does complain about the 'typename' in LINE3 of g(...)
> "typename.c", line 19: error: typename may only be used within a template
> typedef typename Container::value_type value_type; // is this ok?
> ^
> */
I think both reported errors are real.
According to my reading of 14.6 Name Resolution para.5,
"typename" is only valid within a template, while para.6
REQUIRES the use of "typename" in f().
--
Chris Kuan, BHP Information Technology
Concatenate for email: mr gazpacho @ hotmail . com
Phone : +61 2 4275 5555 Fax : +61 2 4275 5547
"A Design Pattern is something that got left out of the language"
- Richard O'Keefe
[ 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.COM (Siemel Naran)
Date: 1998/11/24 Raw View
#include <vector>
//We know that typename is required when we have templates. Eg,
template <class Container>
void f(Container const & c)
{
typedef Container::value_type value_type; // error
typedef typename Container::value_type value_type; // ok
}
//But is the 'typename' keyword ok to use even if it is not required?
void g(std::vector<int> const & c)
{
typedef std::vector<int> Container;
typedef Container::value_type value_type; // ok
typedef typename Container::value_type value_type; // is this ok?
}
//I'm changing my code from a template to a non-template, and I'm lazy
//to take the typename out :). Besides, the 'typename' looks nice
//there as a comment. And it has a cool color on my editor.
int main()
{
f(std::vector<double>());
}
/*
Egcs doesn't mind the 'typename' in LINE3 of function g(...)
It correctly complains about the missing 'typename' in LINE1 of f(...)
typename.c: In function `void f(const Container &)':
typename.c:8: `Container::value_type' is not a valid declarator
typename.c:8: perhaps you want `typename Container::value_type' to make
it a type
typename.c:8: parse error before `;'
*/
/*
Como doesn't complain about the missing 'typename' in LINE1 of f(...)
It does complain about the 'typename' in LINE3 of g(...)
"typename.c", line 19: error: typename may only be used within a template
typedef typename Container::value_type value_type; // is this ok?
^
*/
So who is right? To me, it seems like egcs is right on both counts.
--
----------------------------------
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 ]