C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Templates, Typedefs, Structs....

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Let_Me_Be
Guest





PostPosted: Sun Sep 03, 2006 8:14 pm    Post subject: Templates, Typedefs, Structs.... Reply with quote



Hi, sorry for the Subject, I'm currently having a big problem in my
project. It has a very strict architecture, and I'm geting into
problems with types like this: Pointer< StaticArray<size,
StaticArray<size, Pointer< VariableArray< Pointer< Item<size> > >....
as you can see, the type is realy long, so I created something like
this:

template < uint size > class BaseTypeDefiner
{
public:
typedef Item<size> Item;
typedef Pointer<Item> PItem;
.....
};

Then using BaseTypeDefiner<size>::Instead of the long one above.

The problem is that using BaseTypeDefiner<2>::Item works just fine, but
template < uint size> class MyClass
{
public:
BaseTypeDefiner<size>::Item item;
};
throw an error - BaseTypeDefiner<size>::Item is not a type.

What am I doing wrong?

Thx for help...


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jens Theisen
Guest





PostPosted: Mon Sep 04, 2006 6:32 pm    Post subject: Re: Templates, Typedefs, Structs.... Reply with quote



Let_Me_Be wrote:
Quote:
What am I doing wrong?

Try

public:
typename BaseTypeDefiner<size>::Item item;

It's similar to the FAQ

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

Unfortuately, within a template, you have to help the parser and tell
him that BaseTypeDefiner<size>::Item is a type, because at that point it
can't tell, but needs to tell in order to continue parsing.

Jens

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Hrayr BABAJANYAN
Guest





PostPosted: Mon Sep 04, 2006 6:34 pm    Post subject: Re: Templates, Typedefs, Structs.... Reply with quote



Hi,
Let_Me_Be wrote:
Quote:
Hi, sorry for the Subject, I'm currently having a big problem in my
project. It has a very strict architecture, and I'm geting into
problems with types like this: Pointer< StaticArray<size,
StaticArray<size, Pointer< VariableArray< Pointer< Item<size> > >....
as you can see, the type is realy long, so I created something like
this:

template < uint size > class BaseTypeDefiner
{
public:
typedef Item<size> Item;
typedef Pointer<Item> PItem;
....
};

Then using BaseTypeDefiner<size>::Instead of the long one above.

The problem is that using BaseTypeDefiner<2>::Item works just fine, but
template < uint size> class MyClass
{
public:
BaseTypeDefiner<size>::Item item;
};
throw an error - BaseTypeDefiner<size>::Item is not a type.

What am I doing wrong?

Thx for help...


Well, the compiler does not know whether the
BaseTypeDefiner<size>::Item is a type or not, some compilers do not
support `implicit typename' (in other it can be deprecated), so you
basicly need to explicitly specify that the BaseTypeDefiner<size>::Item
is a typename. [see the code below]

template <uint size> class MyClass
{
public:
typename BaseTypeDefiner<size>::Item item;
};

Cheers!
--
Hrayr


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Maeder
Guest





PostPosted: Mon Sep 04, 2006 6:34 pm    Post subject: Re: Templates, Typedefs, Structs.... Reply with quote

"Let_Me_Be" <Happy.Cerberus (AT) gmail (DOT) com> writes:

Quote:
template < uint size > class BaseTypeDefiner
{
public:
typedef Item<size> Item;
typedef Pointer<Item> PItem;
....
};

Then using BaseTypeDefiner<size>::Instead of the long one above.

The problem is that using BaseTypeDefiner<2>::Item works just fine, but
template < uint size> class MyClass
{
public:
BaseTypeDefiner<size>::Item item;
};
throw an error - BaseTypeDefiner<size>::Item is not a type.

The compiler has to figure out the meaning of the name Item in
BaseTypeDefiner<size>::Item. This is a dependant name; a later
specialization of MyClass could define Item to something different
than the base template.

For dependant names, the compiler has to assume that they do *not*
name a type unless the code explicitly says so, as in:

typename BaseTypeDefiner<size>::Item item;


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jens Kilian
Guest





PostPosted: Mon Sep 04, 2006 6:35 pm    Post subject: Re: Templates, Typedefs, Structs.... Reply with quote

"Let_Me_Be" <Happy.Cerberus (AT) gmail (DOT) com> writes:
Quote:
The problem is that using BaseTypeDefiner<2>::Item works just fine, but
template < uint size> class MyClass
{
public:
BaseTypeDefiner<size>::Item item;
};
throw an error - BaseTypeDefiner<size>::Item is not a type.

What am I doing wrong?

You forgot the 'typename' keyword, which tells the compiler that
BaseTypeDefiner<size>::Item *is* a type.

template <uint size> class MyClass
{
public:
typename BaseTypeDefiner<size>::Item item;
};// ^^^^^^^^

HTH,
Jens.
--
mailto:jjk (AT) acm (DOT) org As the air to a bird, or the sea to a fish,
http://www.bawue.de/~jjk/ so is contempt to the contemptible. [Blake]
http://del.icio.us/jjk

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
John Moeller
Guest





PostPosted: Mon Sep 04, 2006 6:37 pm    Post subject: Re: Templates, Typedefs, Structs.... Reply with quote

Let_Me_Be wrote:
Quote:
Hi, sorry for the Subject, I'm currently having a big problem in my
project. It has a very strict architecture, and I'm geting into
problems with types like this: Pointer< StaticArray<size,
StaticArray<size, Pointer< VariableArray< Pointer< Item<size> > >....
as you can see, the type is realy long, so I created something like
this:

template < uint size > class BaseTypeDefiner
{
public:
typedef Item<size> Item;
typedef Pointer<Item> PItem;
....
};

Then using BaseTypeDefiner<size>::Instead of the long one above.

The problem is that using BaseTypeDefiner<2>::Item works just fine, but
template < uint size> class MyClass
{
public:
BaseTypeDefiner<size>::Item item;
};
throw an error - BaseTypeDefiner<size>::Item is not a type.

What am I doing wrong?

Item is a dependent name of another template, in this case,
BaseTypeDefinter<>, so it needs to be qualified with the "typename"
keyword, like this:

template <uint size> class MyClass
{
public:
// needs "typename" here:
typename BaseTypeDefiner<size>::Item item;
};

There can be ambiguities with dependent type names, because the
compiler often can't determine whether a name is a static member
variable, a member function, a type, etc., so you need to qualify every
dependent name with "typename" if you use it as a type.

If you think that's unwieldy, you can always typedef it again if you
want to be able to use it repeatedly:

template <uint size> class MyClass
{
public:
typedef typename BaseTypeDefiner<size>::Item Item;
Item item;
};


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Tutone
Guest





PostPosted: Mon Sep 04, 2006 6:37 pm    Post subject: Re: Templates, Typedefs, Structs.... Reply with quote

Let_Me_Be wrote:

Quote:
Hi, sorry for the Subject, I'm currently having a big problem in my
project. It has a very strict architecture, and I'm geting into
problems with types like this: Pointer< StaticArray<size,
StaticArray<size, Pointer< VariableArray< Pointer< Item<size> > >....
as you can see, the type is realy long, so I created something like
this:

template < uint size > class BaseTypeDefiner
{
public:
typedef Item<size> Item;
typedef Pointer<Item> PItem;
....
};

Then using BaseTypeDefiner<size>::Instead of the long one above.

The problem is that using BaseTypeDefiner<2>::Item works just fine, but
template < uint size> class MyClass
{
public:
BaseTypeDefiner<size>::Item item;

Change the above line to:

typename BaseTypeDefiner<size>::Item item;

Best regards,

Tom


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ivan Vecerina
Guest





PostPosted: Mon Sep 04, 2006 6:37 pm    Post subject: Re: Templates, Typedefs, Structs.... Reply with quote

"Let_Me_Be" <Happy.Cerberus (AT) gmail (DOT) com> wrote in message

news:1157280550.032652.324660 (AT) e3g2000cwe (DOT) googlegroups.com...

: template < uint size > class BaseTypeDefiner
: {
: public:
: typedef Item<size> Item;
: typedef Pointer<Item> PItem;
: ....
: };
:
: Then using BaseTypeDefiner<size>::Instead of the long one above.
:
: The problem is that using BaseTypeDefiner<2>::Item works just fine, but
: template < uint size> class MyClass
: {
: public:
: BaseTypeDefiner<size>::Item item;
: };
: throw an error - BaseTypeDefiner<size>::Item is not a type.
:
: What am I doing wrong?



You need to use the 'typename' keyword to disambiguate
references to some identifiers within template scope.

In the last class above, you need to write:
typename BaseTypeDefiner<size>::Item item;

There is a technical reason why this is so: when interpreting
the definition of the MyClass template, the compiler cannot know
whether BaseTypeDefiner<size>::Item is a type or another kind
of member, and this would lead to some ambiguities.
'typename' tells the compiler that the referenced identifier
is a type.

{However please look at the settings on your news-reader because I had
to manually delete double line spacing. -mod}

hth -Ivan

--

http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form

Brainbench MVP for C++ <> http://www.brainbench.com







[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ismail Pazarbasi
Guest





PostPosted: Mon Sep 04, 2006 6:40 pm    Post subject: Re: Templates, Typedefs, Structs.... Reply with quote

Let_Me_Be wrote:
Quote:
Hi, sorry for the Subject, I'm currently having a big problem in my
project. It has a very strict architecture, and I'm geting into
problems with types like this: Pointer< StaticArray<size,
StaticArray<size, Pointer< VariableArray< Pointer< Item<size> > >....
as you can see, the type is realy long, so I created something like
this:

template < uint size > class BaseTypeDefiner
{
public:
typedef Item<size> Item;
typedef Pointer<Item> PItem;
....
};

Then using BaseTypeDefiner<size>::Instead of the long one above.

The problem is that using BaseTypeDefiner<2>::Item works just fine, but
template < uint size> class MyClass
{
public:
BaseTypeDefiner<size>::Item item;
};
throw an error - BaseTypeDefiner<size>::Item is not a type.

What am I doing wrong?

Thx for help...


BaseTypeDefiner<T>::Item is a dependant name. Therefore, you should add
"typename" before BaseTypeDefiner<T> in MyClass.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
AlbertSSj
Guest





PostPosted: Tue Sep 05, 2006 6:54 am    Post subject: Re: Templates, Typedefs, Structs.... Reply with quote

Quote:
template < uint size> class MyClass
{
public:
BaseTypeDefiner<size>::Item item;
};
throw an error - BaseTypeDefiner<size>::Item is not a type.

Probably a typename can fix this.
So I'll rewrite like this:

template < uint size> class MyClass
{
public:
typedef typename BaseTypeDefiner<size>::Item Item; // My
preferred way
Item item;
};

Another solution (not the best since you have to rewrite typename all
the times) is

template < uint size> class MyClass
{
public:
typedef typename BaseTypeDefiner<size>::Item item; // I dont
like this one a lot
};

--

An update... I tried this on gcc 3.3 and compile, so this should be the
solution ;)


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
James Dennett
Guest





PostPosted: Tue Sep 05, 2006 5:34 pm    Post subject: Re: Templates, Typedefs, Structs.... Reply with quote

AlbertSSj wrote:
Quote:
template < uint size> class MyClass
{
public:
BaseTypeDefiner<size>::Item item;
};
throw an error - BaseTypeDefiner<size>::Item is not a type.

Probably a typename can fix this.
So I'll rewrite like this:

template < uint size> class MyClass
{
public:
typedef typename BaseTypeDefiner<size>::Item Item; // My
preferred way
Item item;
};

Another solution (not the best since you have to rewrite typename all
the times) is

template < uint size> class MyClass
{
public:
typedef typename BaseTypeDefiner<size>::Item item; // I dont
like this one a lot
};

I think the "typedef" in that last example should be deleted,
as the idea was to define a data member, not a typedef.

-- James

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.