473,808 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::s tring, Tp::ptrsmap;
};
template<class Tp>
std::map<std::s tring, Tp::ptrthemap<T p>::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 1437
ds
On Apr 16, 12:38 pm, Michael DOUBEZ <michael.dou... @free.frwrote:
ds a écrit :
template<class Tp>
std::map<std::s tring, Tp::ptrthemap<T p>::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::s tring, typename Tp::ptrthemap<T p>::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::s tring, typename Tp::ptrsmap;
};
template<class Tp>
std::map<std::s tring,typename Tp::ptrthemap<T p>::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::s tring, typename Tp::ptrsmap;
};
template<class Tp>
std::map<std::s tring,typename Tp::ptrthemap<T p>::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>::p tr must be defined to
define test and test must be defined to define themap<test>::p 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<testcl ass themap
{
public:

typedef int test::*ptr;
static std::map<std::s tring, 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
6399
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. How do I do this? Also, I want to have a data member for
15
3498
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 is a small program I wrote to check if I could get the address of the function from the member-function-pointer, so that I could pass it to the function as a normal function-pointer.
6
3180
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(); TOnMsgReceive *OnMsgReceive; };
34
3697
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 currently use static float gxy;
4
2254
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++) *(arrayPtr+i)*=2;
33
5088
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 actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
1
3983
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 arguments, is this VC specific or a standard c++ behavior? the code looks like this: class Base { public: int member; };
12
540
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 { public: int n; }; The pointer to MyClass.n shall be defined like this:
5
2577
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: const char *s = string_mapper<thetype>(); However I do not know the string to be associated with the type at compile time, and will need a way to set up the mapping, to be created at run time, possibly like so: void foo(char*...
0
9600
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10373
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9195
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.