C++ templates and 'typename'

C++ templates and 'typename'

Post by Nysal J » Fri, 23 Nov 2001 20:07:59



The following piece of code compiles properly using g++ 2.95.3
, but does not compile using Visual age for C++ 5.0.

/* The entire class is not listed here */

template <class T>
class A:public B
{
        private:

        typedef std::list<T*>                             Queue;
        typedef std::map<std::string, Queue::iterator>    ObjectMap;

Quote:};

Visual age gives the following error :
"abc.h", line 55.17: 1540-0152 (W) A template dependent name that is a
ty
pe must be qualified with "typename".
"abc.h", line 56.39: 1540-0714 (S) The template argument must be a
type,
to match the template parameter.

Where should the keyword typename be appended in the above code
snippet.
From what I understood 'typename' should be used in a scenario like
the following one.

template <class T> class xyz {
    void foo() { typename T::x * p; /* ... */ p = blah; }

Quote:};

Here the compiler wouldnt know whether it is a declaration or an
expression (Multiplication).

Regards
Nysal Jan K.A.

 
 
 

C++ templates and 'typename'

Post by Pat Butche » Fri, 23 Nov 2001 20:17:49



> The following piece of code compiles properly using g++ 2.95.3
> , but does not compile using Visual age for C++ 5.0.

> /* The entire class is not listed here */

> template <class T>
> class A:public B
> {
>         private:

>         typedef std::list<T*>                             Queue;
>         typedef std::map<std::string, Queue::iterator>    ObjectMap;
> };
> Visual age gives the following error :
> "abc.h", line 55.17: 1540-0152 (W) A template dependent name that is a
> ty
> pe must be qualified with "typename".
> "abc.h", line 56.39: 1540-0714 (S) The template argument must be a
> type,
> to match the template parameter.

> Where should the keyword typename be appended in the above code
> snippet.

typedef std::map<std::string, typename Queue::iterator> ObjectMap;

Pat

 
 
 

1. Positions of the qualifiers 'typename' and 'template'

In a template which takes a typename C,
if C::member is a type, we should write 'typename C::member', and
if C::member is a template, we should write 'C::template member'
and 'c.template member' where c is of type C.
Thus, 'typename' comes before 'C::' but 'template' after 'C::'.
Why does C++ have this inconsistency?

// Example:

struct C
{
     typedef int type;
     template<typename T> void nonstatic_template_func(const T*);
     template<typename T> static void static_template_func(const T*);

template<class C> void f()
{
     C().template nonstatic_template_func<typename C::type>(0);
     C::template static_template_func<typename C::type>(0);

// End example

--


      [ about comp.lang.c++.moderated. First time posters: do this! ]

2. bootable cdrom

3. MIND? Macinotsh BIND?

4. Must typename appear in template function's argument list or return value?

5. Problem sending Acorn generated PS files to Ghostscript

6. Converting '->' to '.' using a template...

7. French ServPak

8. Problems with 'template specialization' and/or 'value to type mapping'

9. need for 'typename' keyword

10. 'typename' a keyword ?

11. C++, Multiple Levels of Inheritance With Templates Causing 'Debug Assertion Failure'

12. use real properties in C++ objects; avoid 'Get' and 'Set'