Define pointer to member as a template

D

ds

Hi all,

what I try to do is the following:

template<class Tp> class themap
{
public:

typedef int Tp::*ptr;
static std::map<std::string, Tp::ptr> smap;
};
template<class Tp>
std::map<std::string, Tp::ptr> themap<Tp>::smap;

though the typedef compiles, the specialization of the map fails. The
idea is to make it possible to define a pointer to an integer member
of a class and use it in the mappings. Unfortunately I cannot find any
reasonable workaround apart from declaring the typedef and the map in
every class separately. Any ideas?

thanks a lot!

-- dimitris
 
M

Michael DOUBEZ

ds a écrit :
template<class Tp>
std::map<std::string, Tp::ptr> themap<Tp>::smap;

though the typedef compiles, the specialization of the map fails.

You have to indicate that Tp::ptr is a type.

template<class Tp>
std::map<std::string, typename Tp::ptr> themap<Tp>::smap;

Michael
 
D

ds

ds a écrit :



You have to indicate that Tp::ptr is a type.

template<class Tp>
std::map<std::string, typename Tp::ptr> themap<Tp>::smap;

Michael

Hi Michael,

thanks for the reply. You would be correct if I did not actually
define the type! The problem is that

typedef int Tp::*ptr; defines a pointer to integer members of class
Tp. This line compiles fine as well. However, the next line (std::map
memberr) results in

'std::map' : 'Tp::ptr' is not a valid template type argument for
parameter '_Ty' on MSVC.

If I use the typename in the declaration and instantiation of the map
and then typedef the pointer in my classes, like in the following:

template<class Tp> class themap
{
public:
static std::map<std::string, typename Tp::ptr> smap;
};
template<class Tp>
std::map<std::string,typename Tp::ptr> themap<Tp>::smap;

class test : public themap<test>
{
public:
int a;
int b;
typedef int test::*ptr;
};

I get 'ptr' : is not a member of 'test'... plus that the point is to
have the typedef in the template.

Thanks a lot!
 
M

Michael DOUBEZ

ds a écrit :
ds a écrit :
You have to indicate that Tp::ptr is a type.
[snip]
thanks for the reply. You would be correct if I did not actually
define the type! [snip]
template<class Tp> class themap
{
public:
static std::map<std::string, typename Tp::ptr> smap;
};
template<class Tp>
std::map<std::string,typename Tp::ptr> themap<Tp>::smap;

class test : public themap<test>
{
public:
int a;
int b;
typedef int test::*ptr;
};

I get 'ptr' : is not a member of 'test'... plus that the point is to
have the typedef in the template.

Yes, you cannot use Tp:: in themap. Only in functions otherwise you have
a circularity in the definition: themap<test>::ptr must be defined to
define test and test must be defined to define themap<test>::ptr.

The CRTP works only with functions.

Michael
 
D

ds

ds a écrit :
ds a écrit :
though the typedef compiles, the specialization of the map fails.
You have to indicate that Tp::ptr is a type.
[snip]
thanks for the reply. You would be correct if I did not actually
define the type! [snip]
template<class Tp> class themap
{
public:
static std::map<std::string, typename Tp::ptr> smap;
};
template<class Tp>
std::map<std::string,typename Tp::ptr> themap<Tp>::smap;

Yes said:
class test : public themap<test>
{
public:
int a;
int b;
typedef int test::*ptr;
};
I get 'ptr' : is not a member of 'test'... plus that the point is to
have the typedef in the template.

Yes, you cannot use Tp:: in themap. Only in functions otherwise you have
a circularity in the definition: themap<test>::ptr must be defined to
define test and test must be defined to define themap<test>::ptr.

The CRTP works only with functions.

Michael

Hi again Michael and thanks a lot for the clarifications. Though I
risk to become overly stubborn and besides the fact that I somehow get
the feeling that this cannot be done, I am not sure if this is
actually a conceptual error or a language limitation like template
typedefs. My original template should instantiate to

template<test> class themap
{
public:

typedef int test::*ptr;
static std::map<std::string, test::ptr> smap;
};

However the typedef provides only an alias to the real name and does
not define a new type as I would like and I think that this is rather
the problem. Anyway, thanks for the feedback!
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top