470,628 Members | 2,354 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,628 developers. It's quick & easy.

passing pointer to member object as template argument

1
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;
};

class Derived : public Base {};

template <class T,int T::*P> class Tmpl {};

main() {
Tmpl<Derived,&Derived::member> t;
}

I get:
error C2440: 'specialization' : cannot convert from 'int Base::* ' to 'int Derived::* '
Standard conversion from pointer-to-member of base to pointer-to-member of derived is not applied for template arguments.

even this doesn't help:
Tmpl<Derived,(int Derived::*)&Derived::member> t;
gives:
error C2975: 'P' : invalid template argument for 'Tmpl', expected compile-time constant expression

only reinterpret cast works:
Tmpl<Derived,reinterpret_cast<int Derived::*>(&Derived::member)> t;
which I'm not sure if it's safe or not.

can anybody help me with this issue?

thanks in advance
Mar 13 '07 #1
1 3343

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Gert Van den Eynde | last post: by
15 posts views Thread by Albert | last post: by
33 posts views Thread by Ney Andrť de Mello Zunino | last post: by
18 posts views Thread by tbringley | last post: by
reply views Thread by wellingj | last post: by
13 posts views Thread by Phil Bouchard | last post: by
7 posts views Thread by ghulands | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.