How to convert C++ member function pointer to C function pointer?

Pages: 12
And what if I tell you that theoretically the C++ compiler can generate a C function that invokes the C++ member function and let the expression to evaluate to pointer to the compiler generated C function instead of C++ member function pointer.

Ah, so you are compiler theorist, now?

You don’t know what you are talking about, and you are too arrogant to have taken the hints we gave you to save face.

I do know what to do in practice and what not to do in practice.

The quality of code “in practice” is sorely lacking, in large part because of low-level mooks like yourself. I don’t care how long you have been doing this kind of stuff. Such code is bad and people who actually know their stuff wouldn’t touch it with a ten foot pole.

I hope I never have the displeasure of ever running your software on anything.


Because you are unwilling to learn.
@Duothomhas,

At least this OP didn't fling profanity and threats of violence when he got push back to his "I'm the expert" arrogance.
Didn't say that getting a C function pointer from C++ member function pointer is always good.

I just was curious to know if this is possible or not.

I didn't decide when this is good to do that or not.

Of course you never have to do that and in most cases this is not much beneficial.
Last edited on
> Didn't say that getting a C function pointer from C++ member function pointer is always good.
> I just was curious to know if this is possible or not.

It is obviously possible; all one has to do is to synthesise a non-member wrapper function which forwards the call to the member function. In pure C++ code, using the standard call wrapper std::function<> is much better.

A very simple version, without any adornments for syntactic sugar, without perfect forwarding etc.:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <functional>

namespace detail
{
    template < typename T, typename R, typename... A > struct ptr_fun
    {
        template < R (T::*PMFN)(A...) > struct fun_maker
        {
            static R fun( T* p, A... args ) { return (p->*PMFN)(args...) ; }
            static auto ptr() { return &fun ; }
        };

        template < R (T::*PMFN)(A...) const > struct fun_maker_c
        {
            static R fun( const T* p, A... args ) { return (p->*PMFN)(args...) ; }
            static auto ptr() { return &fun ; }
        };
    };
}

struct A
{
    double foo( int a, char b ) { std::cout << "A::foo(int,char)\n" ; return double(a) + b ; }
    int bar( short a, char b, char c ) const { std::cout << "A::bar(short,char,char) const\n" ; return int(a) + b + c ; }
};

int main()
{
    // pointer to free function
    const auto pfn_foo = detail::ptr_fun< A, double, int, char >::fun_maker< &A::foo >::ptr() ;
    // call wrapper
    const auto foo_wrapped = std::function< double( A*, int, char ) >( &A::foo ) ;

    // pointer to free function
    const auto pfn_bar = detail::ptr_fun< A, int, short, char, char >::fun_maker_c< &A::bar >::ptr() ;
    // call wrapper
    const auto bar_wrapped = std::function< int( const A*, short, char, char ) >( &A::bar ) ;

    A a ;
    pfn_foo( &a, 1, 'a' ) ; // A::foo(int,char)
    foo_wrapped( &a, 1, 'a' ) ; // A::foo(int,char)

    const A ca ;
    pfn_bar( &ca, 2, 'b', 'c' ) ; // A::bar(short,char,char) const
    bar_wrapped( &ca, 2, 'b', 'c' ) ; // A::bar(short,char,char) const
}

http://coliru.stacked-crooked.com/a/7359c3583b82a026
https://rextester.com/JRERAQ76407
Interesting. Thanks.
Topic archived. No new replies allowed.
Pages: 12