By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

passing pointer to member object as template argument

P: 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
Share this Question
Share on Google+
1 Reply

Post your reply

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