 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mikhail Parakhin Guest
|
Posted: Sat Sep 23, 2006 9:43 pm Post subject: typedefs in CRTP - what's wrong? |
|
|
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
|
Posted: Sun Sep 24, 2006 12:11 am Post subject: Re: typedefs in CRTP - what's wrong? |
|
|
"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
|
Posted: Sun Sep 24, 2006 12:11 am Post subject: Re: typedefs in CRTP - what's wrong? |
|
|
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 |
|
 |
|
|
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
|
|