How to convert a pointer to member function type to a const member one
  Home FAQ Contact Sign in
Your Ad Here
comp.lang.c++.moderated only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c++.moderated Profile…

 Up
How to convert a pointer to member function type to a const member one         


Author: mcostalba
Date: Apr 30, 2008 08:43

Hi all,

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 have tried

typdef T const TC

but with no success.

Thanks
Marco

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
12 Comments
Re: How to convert a pointer to member function type to a const member one         


Author: nickf3
Date: Apr 30, 2008 15:11

On Apr 30, 12:38 pm, mcostalba gmail.com> wrote:
> Hi all,
>
> 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 have tried
>
> typdef T const TC
>
> but with no success.
>
> Thanks ...
Show full article (0.76Kb)
no comments
Re: How to convert a pointer to member function type to a const member one         


Author: smolensky
Date: Apr 30, 2008 20:19

On Apr 30, 12:38 pm, mcostalba gmail.com> wrote:
> Hi all,
>
> 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 have tried
>
> typdef T const TC
>
> but with no success.
>
Show full article (1.22Kb)
no comments
Re: How to convert a pointer to member function type to a const member one         


Author: Devdatt Lad
Date: Apr 30, 2008 20:19

> 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

These two types are completely different. A better way to understand the
meaning of these types is:

T ==> int (foo::*)(foo obj, char c)
TC ==> int (foo::*)(const foo obj, char c)

Thus, it is not the member function pointer which is const or non-const. It
is actually an argument of the member function that differs in const-ness.
This cannot be done with a static_cast or a const_cast.

You can use a reinterpret_cast to do this.

T x = &foo::abc;
TC y = reinterpret_cast(x);

but be careful when using the result of reinterpret_cast.

Hope this helps,
Devdatt
Show full article (0.85Kb)
no comments
Re: How to convert a pointer to member function type to a const member one         


Author: Greg 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;
Show full article (1.60Kb)
no comments
Re: How to convert a pointer to member function type to a const member one         


Author: mcostalba
Date: May 1, 2008 11:51

On 1 Mag, 06:17, Greg Herlihy mac.com> wrote:
>
> 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.

Yes, this is similar to what I have to do, actually I have to do the
contrary: detect pointer to const member functions.
Show full article (2.32Kb)
no comments
Re: How to convert a pointer to member function type to a const member one         


Author: Roman.Perepelitsa
Date: May 5, 2008 05:48

On 1 May, 21:43, mcostalba gmail.com> wrote:
> I have to detect if a functor type Fun has a given signature Sig, i.e.
> if Fun::operator() has a signature Sig

Take a look at is_call_possible metafunction, which was discussed
in this group before
(http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/
thread/4f7c7a96f9afbe44/e5fbc9305539f699#e5fbc9305539f699).
It seems to do what you want.

HTH,
Roman Perepelitsa.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
no comments
Re: How to convert a pointer to member function type to a const member one         


Author: mcostalba
Date: May 5, 2008 23:00

On 5 Mag, 15:09, "Roman.Perepeli...@gmail.com"
gmail.com> wrote:
> On 1 May, 21:43, mcostalba gmail.com> wrote:
>
>> I have to detect if a functor type Fun has a given signature Sig, i.e.
>> if Fun::operator() has a signature Sig
>
> Take a look at is_call_possible metafunction, which was discussed
> in this group before
> (http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/
> thread/4f7c7a96f9afbe44/e5fbc9305539f699#e5fbc9305539f699).
> It seems to do what you want.
>

Thanks Roman for your link.

Actually there are some good ideas in that implementation and I will
try to borrow a few of them...

Unfortunatly it does not fit the bill 100%%, indeed:
Show full article (1.86Kb)
no comments
Re: How to convert a pointer to member function type to a const member one         


Author: Roman.Perepelitsa
Date: May 6, 2008 06:30

On 6 May, 08:57, mcostalba gmail.com> wrote:
> Thanks Roman for your link.
>
> Actually there are some good ideas in that implementation and I will
> try to borrow a few of them...
>
> Unfortunatly it does not fit the bill 100%%, indeed:
>
> - Implicit argument type conversion should be avoided in my context,
> due to some ambiguites that can arise when there is more then one
> candidate among the overload set. So signature matching would be ok to
> be strict.

Alexandre Courpron modified is_call_possible to create
is_exact_call_possible. You can find implementation here:
http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_thread/thread/bd...#a56a91737e3f8c7c
> - It seems you have to add a specialization for each argument arity,
> in my implementation I would try to avoid this.

Yes, you can use variadic templates (if your compiler supports then)
or boost::preprocessor to automate generation of specializations.
Show full article (1.79Kb)
no comments
Re: How to convert a pointer to member function type to a const member one         


Author: mcostalba
Date: May 6, 2008 11:14

On 6 Mag, 16:21, "Roman.Perepeli...@gmail.com"
gmail.com> wrote:
>
>
> Alexandre Courpron modified is_call_possible to create
> is_exact_call_possible. You can find implementation here:http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_threa...

I have quickly checked the link and it surely deserves a deeper read
when I found a bit of time.

Anyhow here is my much simpler solution...so simple that is probably
broken in some way, but I cannot find where:

/* Check if a function/functor Fun has a given signature Sig */
template
struct is_compatible
{
typedef typename boost::remove_pointer::type F;
Show full article (2.64Kb)
no comments

RELATED THREADS
SubjectArticles qty Group
Re: To convert or not to convert, that's the questioncomp.lang.clipper.visual-objects ·
Re: Global Const vs Public Constmicrosoft.public.vb.general.discussion ·
1 2