typenames in self-referential templates

A

Anthony Heading

Hi all,
I've often found myself wanting to write code like the example here.
Since
both MSVC and gcc both reject it, I suspect it is indeed illegal.

gcc: no type named `Name' in `class Collection<Animal>'
msvc7: error C2039: 'Name' : is not a member of 'Collection<Traits>'

But to me it seems pretty unambiguous, so I can't see why it's wrong.
Could anybody give me a pointer, either to the standard or the basic
rationale why it's an error?

Thanks

Anthony


template <class T>
class Animal
{
typename T::Name name; // This seems to be an ERROR
};

template <
class Collection :
public Traits < Collection<Traits> >
{
public:
typedef char* Name;
};

int main()
{
Collection<Animal> zoo;
return 0;
}
 
D

David B. Held

Anthony Heading said:
[...]
template <class T>
class Animal
{
typename T::Name name; // This seems to be an ERROR
[...]

Should there be a typedef in there somewhere? I wasn't aware
that 'typename' was allowed to start a declaration.

Dave
 
G

Gianni Mariani

David said:
[...]
template <class T>
class Animal
{
typename T::Name name; // This seems to be an ERROR
[...]


Should there be a typedef in there somewhere? I wasn't aware
that 'typename' was allowed to start a declaration.

typename is used as a way to make templates unambiguous.

without typename the code would look like:

T::Name name;

A declaration is

TYPE <DECL> ;

However there is no way for the compiler to know that T::Name is a type.

So "typename T::Name" is a way of indicating that it IS a type.
 
G

Gianni Mariani

Anthony said:
Hi all,
I've often found myself wanting to write code like the example here.
Since
both MSVC and gcc both reject it, I suspect it is indeed illegal.

gcc: no type named `Name' in `class Collection<Animal>'
msvc7: error C2039: 'Name' : is not a member of 'Collection<Traits>'

But to me it seems pretty unambiguous, so I can't see why it's wrong.
Could anybody give me a pointer, either to the standard or the basic
rationale why it's an error?

The code below does not compile.

Try posting the code you really intended to.
 
M

Mike Wahler

Gianni Mariani said:
David said:
[...]
template <class T>
class Animal
{
typename T::Name name; // This seems to be an ERROR
[...]


Should there be a typedef in there somewhere? I wasn't aware
that 'typename' was allowed to start a declaration.

typename is used as a way to make templates unambiguous.

without typename the code would look like:

T::Name name;

A declaration is

TYPE <DECL> ;

However there is no way for the compiler to know that T::Name is a type.

So "typename T::Name" is a way of indicating that it IS a type.

Also, note that in some contexts, it's not ambiguous
at all what the type is, but the language rules still
demand a 'typename' keyword.

-Mike
 
S

Shane Beasley

Anthony Heading said:
template <class T>
class Animal
{
typename T::Name name; // This seems to be an ERROR
};

template <

class Collection :
public Traits < Collection<Traits> >
{
public:
typedef char* Name;
};

Collection<Animal> depends on Animal<Collection> (via inheritance),
int main()
{
Collection<Animal> zoo;
return 0;
}

Collection's template parameter is called Traits, but Animal isn't a
traits class -- it's the type of element stored in the Collection.
You're actually using Collection as a traits class for Animal, except
that Collection is defined in terms of Animal. Of course, it doesn't
make sense for a contained type to depend on the definition of its
container, anyway.

Perhaps there should be a third class with traits for Animal, such
that Collection< Animal<Traits> > would be used instead. More likely,
Animal should either have a hardcoded Name type, or it should be some
kind of global (or scoped) typedef -- are you really going to use
Animals with different types of Name in your program?

- Shane
 
D

David B. Held

Gianni Mariani said:
David said:
[...]
template <class T>
class Animal
{
typename T::Name name; // This seems to be an ERROR
[...]


Should there be a typedef in there somewhere? I wasn't aware
that 'typename' was allowed to start a declaration.

typename is used as a way to make templates unambiguous.

without typename the code would look like:

T::Name name;
[...]

Ah. Usually, people typedef dependent names, so I've never seen
typename used in a declaration before.

Dave
 
A

Anthony Heading

Shane Beasley said:
Collection<Animal> depends on Animal<Collection> (via inheritance),
but Animal<Collection> depends on Collection<Animal> (via T::Name).
Can't do that.

So I infer... but the rationale escapes me. Consider for contrast the code
attached below, which _does_ run fine.
It seems that function names are allowed to have such a circular dependency
in templates, but not types.
Why fundamentally is this OK, but the types are a no-can-do?
Perhaps there should be a third class with traits for Animal, such
that Collection< Animal<Traits> > would be used instead. More likely,
Animal should either have a hardcoded Name type, or it should be some
kind of global (or scoped) typedef -- are you really going to use
Animals with different types of Name in your program?

I am. Obviously this is somewhat of a toy example, but one could imagine
Asian animals whose
Names are wchar_t. Clearly these would have need a Collection zoo of their
own, since the
type signature is different, but yes I could have both in the same program.

With a real program, I have templates mixed into a single class, and I just
find it puzzling
that the _functions_ can happily cross-call each other by passing in the
aggregate class as
a template param to its constituents, but the _types_ cannot be used that
way.

Rgds

Anthony

#include <iostream>

template <class T>
class Animal
{
public:
Animal() {
std::cout << T::something() << std::endl;
}
};

template <
class Collection :
public Traits < Collection<Traits> >
{
public:
typedef char* Name;
static Name something() {
return (Name) "Menagerie";
}
};

int main()
{
Collection<Animal> zoo;
return 0;
}
 
S

Shane Beasley

Anthony Heading said:
So I infer... but the rationale escapes me. Consider for contrast the code
attached below, which _does_ run fine.
It seems that function names are allowed to have such a circular dependency
in templates, but not types.
Why fundamentally is this OK, but the types are a no-can-do?

Actually, types are fine, too. What you did differently here was put
the use inside a function. Functions are exempt from this rule because
the compiler can pretend as though they are processed out of line,
whereas it can't generally do the same for declarations in the class.

For instance, this code compiles as well:

template <typename T> struct Animal {
void f () { typedef typename T::Name name; }
};

template <template <class> class T>
struct Collection : T< Collection<T> > {
typedef char *Name;
};

int main () { Collection<Animal>().f(); }

The reason this works is because the compiler can reinterpret Animal
thus:

template <typename T> struct Animal { void f (); };

template <typename T>
inline void Animal<T>::f () {
typedef typename T::Name name;
}

Note that the mutual dependence between the class definitions is gone
-- the definition of Animal does not use T at all. As a result,
Animal< Collection<Animal> > and Collection<Animal> can be fully
processed, and then Animal::f() can work with the result.

Note, also, that this only applies to the bodies of the functions. For
instance, you can't do this:

template <typename T> struct Animal {
void f (typename T::Name *);
};

This reintroduces Animal's dependence on T.
I am. Obviously this is somewhat of a toy example, but one could imagine
Asian animals whose
Names are wchar_t. Clearly these would have need a Collection zoo of their
own, since the
type signature is different, but yes I could have both in the same program.

It's hard for me to critique a toy program in a way that applies to
the real program it models, so I'll leave that alone. :)
With a real program, I have templates mixed into a single class, and I just
find it puzzling
that the _functions_ can happily cross-call each other by passing in the
aggregate class as
a template param to its constituents, but the _types_ cannot be used that
way.

Again, the classes' definitions cannot be mutually independent, though
their member functions' definitions can, even when they use dependent
types.

BTW, the tried-and-true way to resolve recursive dependence is to add
a level of abstraction:

template <typename T>
struct Animal { typedef typename T::Name name; };

struct CollectionTraits { typedef const char *Name; };

template <template <class> class T>
struct Collection : Animal<CollectionTraits> {
};

- Shane
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top