Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Define pointer to member as a template

Reply
Thread Tools

Define pointer to member as a template

 
 
ds
Guest
Posts: n/a
 
      04-16-2008
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:tr> smap;
};
template<class Tp>
std::map<std::string, Tp:tr> 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
 
Reply With Quote
 
 
 
 
Michael DOUBEZ
Guest
Posts: n/a
 
      04-16-2008
ds a écrit :

> template<class Tp>
> std::map<std::string, Tp:tr> themap<Tp>::smap;
>
> though the typedef compiles, the specialization of the map fails.


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

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

Michael
 
Reply With Quote
 
 
 
 
ds
Guest
Posts: n/a
 
      04-16-2008
On Apr 16, 12:38 pm, Michael DOUBEZ <(E-Mail Removed)> wrote:
> ds a écrit :
>
> > template<class Tp>
> > std::map<std::string, Tp:tr> themap<Tp>::smap;

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

>
> You have to indicate that Tp:tr is a type.
>
> template<class Tp>
> std::map<std::string, typename Tp:tr> 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:tr' 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:tr> smap;
};
template<class Tp>
std::map<std::string,typename Tp:tr> 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!
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      04-16-2008
ds a écrit :
> On Apr 16, 12:38 pm, Michael DOUBEZ <(E-Mail Removed)> wrote:
>> ds a écrit :
>>> though the typedef compiles, the specialization of the map fails.

>> You have to indicate that Tp:tr 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:tr> smap;
> };
> template<class Tp>
> std::map<std::string,typename Tp:tr> themap<Tp>::smap;


Yes, typename should be used in themap<> also.


> 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>:tr must be defined to
define test and test must be defined to define themap<test>:tr.

The CRTP works only with functions.

Michael
 
Reply With Quote
 
ds
Guest
Posts: n/a
 
      04-16-2008
On Apr 16, 1:07 pm, Michael DOUBEZ <(E-Mail Removed)> wrote:
> ds a écrit :
>
> > On Apr 16, 12:38 pm, Michael DOUBEZ <(E-Mail Removed)> wrote:
> >> ds a écrit :
> >>> though the typedef compiles, the specialization of the map fails.
> >> You have to indicate that Tp:tr 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:tr> smap;
> > };
> > template<class Tp>
> > std::map<std::string,typename Tp:tr> themap<Tp>::smap;

>
> Yes, typename should be used in themap<> also.
>
> > 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>:tr must be defined to
> define test and test must be defined to define themap<test>:tr.
>
> 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:tr> 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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM



Advertisments