473,396 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Define pointer to member as a template

ds
Hi all,

what I try to do is the following:

template<class Tpclass themap
{
public:

typedef int Tp::*ptr;
static std::map<std::string, Tp::ptrsmap;
};
template<class Tp>
std::map<std::string, Tp::ptrthemap<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
Jun 27 '08 #1
2 1408
ds
On Apr 16, 12:38 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
ds a écrit :
template<class Tp>
std::map<std::string, Tp::ptrthemap<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::ptrthemap<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 Tpclass themap
{
public:
static std::map<std::string, typename Tp::ptrsmap;
};
template<class Tp>
std::map<std::string,typename Tp::ptrthemap<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!
Jun 27 '08 #2
ds
On Apr 16, 1:07 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
ds a écrit :
On Apr 16, 12:38 pm, Michael DOUBEZ <michael.dou...@free.frwrote:
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 Tpclass themap
{
public:
static std::map<std::string, typename Tp::ptrsmap;
};
template<class Tp>
std::map<std::string,typename Tp::ptrthemap<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>::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<testclass themap
{
public:

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

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!
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gert Van den Eynde | last post by:
Hi all, A beginners question.... I've got a template class template <class T> classA {...} In an other class, I want to pass a pointer to an instance of classA as a function argument....
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
6
by: | last post by:
Say we have the following code defining TMyMsgHandler and TMyClass typedef void (*TOnMsgReceive) (TMyMessage Msg); class TMyMsgHandler { public: TMyMsgHandler(); virtual ~TMyMsgHandler();...
34
by: Dennis | last post by:
I would like to dynamically allocate in a sub a 2 dimensional Array float *myarray = new float ; of course I get an error. How do you allocate a 2D array using the New operator? I...
4
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
1
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template...
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
5
by: alan | last post by:
Hello world, I'm wondering if it's possible to implement some sort of class/object that can perform mapping from class types to strings? I will know the class type at compile time, like so:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.