Topic: Regarding "typename


Author: bgera@hotmail.com (Bond)
Date: Thu, 7 Feb 2002 18:27:02 GMT
Raw View
bgera@hotmail.com (Bond) wrote in message news:<8e2f7846.0202042240.36ea1ac6@posting.google.com>...
> Im curious to know if keyword "class" can in all instances replace the
> keyword "typename".
> thanks
>
> ---
> [ 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.research.att.com/~austern/csc/faq.html                ]

Thanks for all the responses.

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: bgera@hotmail.com (Bond)
Date: Tue, 5 Feb 2002 06:44:57 GMT
Raw View
Im curious to know if keyword "class" can in all instances replace the
keyword "typename".
thanks

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Richard Smith" <richard@ex-parrot.com>
Date: Tue, 5 Feb 2002 17:38:36 GMT
Raw View
"Bond" <bgera@hotmail.com> wrote in message
news:8e2f7846.0202042240.36ea1ac6@posting.google.com...
> Im curious to know if keyword "class" can in all instances replace the
> keyword "typename".

No.  Typename can be used (I think) in two circumstaces: i) when declaring
template parameters, and ii) when using types that are dependent on a
template parameter.

    // (i) typename declares a template parameter
    template <typename T> class foo { /* ... */ };

    // (ii) using a type that is dependent on a template parameter
    template <bool b> class foo
    {
      typedef typename traits<b>::type type;
    };

Usage (i) can be replaced with class, usage (ii) cannot.

--
Richard Smith



---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: comeau@panix.com (Greg Comeau)
Date: Tue, 5 Feb 2002 17:48:46 GMT
Raw View
In article <8e2f7846.0202042240.36ea1ac6@posting.google.com>,
Bond <bgera@hotmail.com> wrote:
>Im curious to know if keyword "class" can in all instances replace the
>keyword "typename".

No.

Check out http://www.comeaucomputing.com/techtalk/templates/#typename
--
Greg Comeau  GA BETA:4+ New Windows Backends  PLUS 'export' beta online!
Comeau C/C++ ONLINE ==>     http://www.comeaucomputing.com/tryitout
World Class Compilers:  Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "Ivica Siladic" <ivica.siladic@mireo.hr>
Date: Tue, 5 Feb 2002 17:52:36 GMT
Raw View
> Im curious to know if keyword "class" can in all instances replace the
> keyword "typename".
> thanks

By definition, keyword "typename" denotes that the identifier following
typename keyword is name of the type. There are circumstances that
you cannot use keyword "class" instead, and more, you must use typename,
as in the following example:

template <class C>
void f(C& c){
    C::iterator it; // error, compiler may have several choices to deduce
                         // what is C::iterator
    typename C::iterator it; // OK, since complier now knows that
                        // C::iterator is name of the type
}

    Ivica Siladic


---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Tue, 5 Feb 2002 17:53:13 GMT
Raw View
In article <8e2f7846.0202042240.36ea1ac6@posting.google.com>, Bond
<bgera@hotmail.com> writes
>Im curious to know if keyword "class" can in all instances replace the
>keyword "typename".

No, there is a single circumstance where class (but not struct) is
interchangeable with typname and that is in marking template parameters
as being type parameters.


--
Francis Glassborow
Check out the ACCU Spring Conference 2002
4 Days, 4 tracks, 4+ languages, World class speakers
For details see: http://www.accu.org/events/public/accu0204.htm

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 5 Feb 2002 17:53:58 GMT
Raw View
Bond wrote:
>
> Im curious to know if keyword "class" can in all instances replace the
> keyword "typename".
> thanks

No. The keyword "typename" has two different uses. One of them is to
introduce template arguments that identify a type, and it can be
replaced with "class" in that context. However, it's also used to
identify the fact that an identifier which is dependent on a template
parameter is a type, (rather than a data or function member). Example:

template<typename T> class Example{
 typedef typename T::A TA;
};

The two 'typename's in that example serve different, but related,
purposes.

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@sensor.com>
Date: Tue, 5 Feb 2002 18:09:35 GMT
Raw View

Bond wrote:
>
> Im curious to know if keyword "class" can in all instances replace the
> keyword "typename".
> thanks

Certainly not.  They are only interchangeable in declaring
template variables:

 template <class T> ...
and
 template <typename T> ...

are interchangeable (even if the type doesn't end up being a
class).

Of course, typename also gets used within templates to disambiguate
types from members, you can't put class there.

---
[ 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.research.att.com/~austern/csc/faq.html                ]





Author: Marco Manfredini <marco@technoboredom.net>
Date: Tue, 5 Feb 2002 20:50:12 GMT
Raw View
bgera@hotmail.com (Bond) wrote:

> Im curious to know if keyword "class" can in all instances replace
> the keyword "typename".

It can't.

class A
{
public:
 typedef int X;
};
template<typename T> // [1]
class B
{
public:
 typename A::X x; // [2] you can't use class here.
};
B<A> b;

'typename' has two applications: [1] to designate template type
parameters (where it works as a synonym to 'class'), [2] to tag the
result of a scope (::) expression as a type (and not a constant or
whatever). 'typename' cannot be replaced with 'class' here.

Marco

---
[ 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.research.att.com/~austern/csc/faq.html                ]