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 

typedefs in CRTP - what's wrong?

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





PostPosted: Sat Sep 23, 2006 9:43 pm    Post subject: typedefs in CRTP - what's wrong? Reply with quote



I came across the following problem recently:

template<class P_Derived>
class T_Base
{
public:
typedef typename P_Derived::t_type t_type; //error ?!!
};

class C_Derived : public T_Base<C_Derived>
{
public:
typedef int t_type;
};

MSVC 8.0 wouldn't compile this code - says: "t_type" is not a member of
C_Derived. That is a bit puzzling - if it can call functions, why can't it
see typedefs? Is it a standard behavior or compiler issue?

Mikhail Parakhin
[I do not want replies, please followup to the group.]



[ 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: Sun Sep 24, 2006 12:11 am    Post subject: Re: typedefs in CRTP - what's wrong? Reply with quote



"Mikhail Parakhin" <clcppm-poster (AT) this (DOT) is.invalid> writes:

Quote:
MSVC 8.0 wouldn't compile this code - says: "t_type" is not a member of
C_Derived. That is a bit puzzling - if it can call functions, why can't it
see typedefs? Is it a standard behavior or compiler issue?

I'm afraid it's standard behaviour, and you can see typedefs in the
same way you can see functions, ie.:

template<class P_Derived>
class T_Base
{
public:
void foo()
{
typedef typename P_Derived::t_type t_type; //works
}
};

class C_Derived : public T_Base<C_Derived>
{
public:
typedef int t_type;
};

For declarations in class scope, however, only those names which have
already been declared can be seend. In case of CRTP, these are none at
all. This also applies to functions, but they are less often referred
to at class scope.

I don't know of nice workarounds I'm afraid, maybe someone else does?

Regards,

Jens

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





PostPosted: Sun Sep 24, 2006 12:11 am    Post subject: Re: typedefs in CRTP - what's wrong? Reply with quote



Mikhail Parakhin wrote:
Quote:
I came across the following problem recently:

template<class P_Derived
class T_Base
{
public:
typedef typename P_Derived::t_type t_type; //error ?!!
};

class C_Derived : public T_Base<C_Derived
{
public:
typedef int t_type;
};

MSVC 8.0 wouldn't compile this code - says: "t_type" is not a member of
C_Derived. That is a bit puzzling - if it can call functions, why can't it
see typedefs? Is it a standard behavior or compiler issue?

The class template specialization T_Base<C_Derived> is being
instantiated while C_Derived is still an incomplete type. And the
typedef declaration must be resolved during that instantiation.

On the other hand, if T_Base<C_Derived> had a member function,
only it's declaration would be instantiated and the definition may
be instantiated later, when C_Derived is a complete type; or it
may not be instantiated at all (if not used).

Regards
Mgr. Vladimir Marko


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