473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

typenames in self-referential templates

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 <
template <class> class Traits

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

int main()
{
Collection<Animal> zoo;
return 0;
}
Jul 19 '05 #1
9 2386
"Anthony Heading" <an*****@magix.com.sg> wrote in message
news:bk**********@catv02.starcat.ne.jp...
[...]
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
Jul 19 '05 #2
David B. Held wrote:
"Anthony Heading" <an*****@magix.com.sg> wrote in message
news:bk**********@catv02.starcat.ne.jp...
[...]
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.

Jul 19 '05 #3
Anthony Heading wrote:
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.

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

template <
template <class> class Traits

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

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


Jul 19 '05 #4
WW
Gianni Mariani wrote:
Anthony Heading wrote:
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.


Great God! He posted the code to ask why doesn't it compile!!!

--
WW aka Attila
Jul 19 '05 #5

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bk********@dispatch.concentric.net...
David B. Held wrote:
"Anthony Heading" <an*****@magix.com.sg> wrote in message
news:bk**********@catv02.starcat.ne.jp...
[...]
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
Jul 19 '05 #6
"Anthony Heading" <an*****@magix.com.sg> wrote in message news:<bk**********@catv02.starcat.ne.jp>...
template <class T>
class Animal
{
typename T::Name name; // This seems to be an ERROR
};

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


Collection<Animal> depends on Animal<Collection> (via inheritance),
but Animal<Collection> depends on Collection<Animal> (via T::Name).
Can't do that.
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
Jul 19 '05 #7
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bk********@dispatch.concentric.net...
David B. Held wrote:
"Anthony Heading" <an*****@magix.com.sg> wrote in message
news:bk**********@catv02.starcat.ne.jp...
[...]
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
Jul 19 '05 #8

"Shane Beasley" <sb******@cs.uic.edu> wrote in message
news:2f**************************@posting.google.c om...
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 <
template <class> class Traits

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

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


Jul 19 '05 #9
"Anthony Heading" <an*****@magix.com.sg> wrote in message news:<bk**********@catv02.starcat.ne.jp>...
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?


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.
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.


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
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
15
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
1
by: Blace Ice | last post by:
HI, Pls. see the following function declaration: int foo(const char *); How to get the return typename and parameter typenames from the declaration? For example, I need some codes like the...
18
by: Ralf W. Grosse-Kunstleve | last post by:
My initial proposal (http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html) didn't exactly get a warm welcome... And Now for Something Completely Different: class autoinit(object):...
4
by: David Coffin | last post by:
I'd like to subclass int to support list access, treating the integer as if it were a list of bits. Assigning bits to particular indices involves changing the value of the integer itself, but...
4
by: marek.rocki | last post by:
First of all, please don't flame me immediately. I did browse archives and didn't see any solution to my problem. Assume I want to add a method to an object at runtime. Yes, to an object, not a...
7
by: Andrew Robert | last post by:
Hi Everyone, I am having a problem with a class and hope you can help. When I try to use the class listed below, I get the statement that self is not defined. test=TriggerMessage(data) var...
24
by: Peter Maas | last post by:
The Python FAQ 1.4.5 gives 3 reasons for explicit self (condensed version): 1. Instance variables can be easily distinguished from local variables. 2. A method from a particular class can be...
84
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to...
6
by: Bart Kastermans | last post by:
I am playing with some trees. In one of the procedures I wrote for this I am trying to change self to a different tree. A tree here has four members (val/type/left/right). I found that self = SS...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.