| Re: How to convert a pointer to member function type to a const member one |
|
 |
|
 |
|
 |
|
 |
Group: comp.lang.c++.moderated · Group Profile
Author: Greg HerlihyGreg Herlihy Date: Apr 30, 2008 20:28
On Apr 30, 9:38 am, mcostalba gmail.com> wrote:
> I have a problem.
>
> What is the best way to get a type TC from a type T where
>
> T is int(foo::*)(char)
>
> and
>
> TC is int(foo::*)(char) const
I'm assuming that you want to "synthesize" a member function pointer
type for a const class method - given a member function pointer for an
equivalent non-const class method. (Otherwise, it is not possible to
convert actual member pointer instances from one type to the another.)
In order to synthesize the const member function pointer type from the
non-const equivalent, I would declare a "helper" template like so:
// General helper template declared - but not defined
template
struct AddConstToMemberFunctionPtr;
// specialized for member function pointers only
template
struct AddConstToMemberFunctionPtr
{
typedef R (T::*type)(P) const;
};
The embedded "type" typedef in the above template is the desired type.
A program could then refer to this const member function pointer type
(most likely inside a template definition) like so:
typename AddConstToMemberFunctionPtr::type;
(with MF being the non-const member function pointer type). Note that
the keyword "typename" in the above example may or may not be required
- depending on whether this expression appears within a template
definition or not.
Greg
|