Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8781793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:23:55+00:00 2026-06-13T20:23:55+00:00

I have a third-party library which has a method that takes a function pointer

  • 0

I have a third-party library which has a method that takes a function pointer as the first parameter:

int third_party_method(void (*func)(double*, double*, int, int, double*), ...);

I want to pass a pointer to a class’ method that is declared as follows:

class TestClass
{
    public:
        void myFunction (double*, double*, int, int, void*);

I tried to pass this function as follows:

TestClass* tc = new TestClass();
using namespace std::placeholders;
third_party_method(std::bind(&TestClass::myFunction, tc, _1, _2, _3, _4, _5), ...);

However, this does not compile:

Conversion of parameter 1 from 'std::tr1::_Bind<_Result_type,_Ret,_BindN>' to 'void (__cdecl *)(double *,double *,int,int,void *)' is not possible
with
[
    _Result_type=void,
    _Ret=void,
    _BindN=std::tr1::_Bind6<std::tr1::_Callable_pmf<void (__thiscall TestClass::* const )(double *,double *,int,int,void *),TestClass,false>,TestClass *,std::tr1::_Ph<1>,std::tr1::_Ph<2>,std::tr1::_Ph<3>,std::tr1::_Ph<4>,std::tr1::_Ph<5>>
]

Is there any way I can pass the member to the function?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:23:56+00:00Added an answer on June 13, 2026 at 8:23 pm

    Is there any way I can pass the member to the function?

    Unless your class object is some kind of global object – it is not possible. Because objects may contain some data, while function pointer is just pointer to function – it doesn’t contain any runtime context, only compile-time one.

    If you accept having compile-time unique IDs for each callback passing, then you can use following generalized approach.

    Usage:

    void test(void (*fptr)())
    {
        fptr();
    }
    
    struct SomeStruct
    {
        int data;
        void some_method()
        {
            cout << data << endl;
        }
        void another_method()
        {
            cout << -data << endl;
        }
    };
    
    int main()
    {
        SomeStruct local[] = { {11}, {22}, {33} };
    
        test(get_wrapper<0>(  boost::bind(&SomeStruct::some_method,local[0]) ));
        test(get_wrapper<1>(  boost::bind(&SomeStruct::another_method,local[0]) ));
    
        test(get_wrapper<2>(  boost::bind(&SomeStruct::some_method,local[1]) ));
        test(get_wrapper<3>(  boost::bind(&SomeStruct::another_method,local[1]) ));
    
        test(get_wrapper<4>(  boost::bind(&SomeStruct::some_method,local[2]) ));
        test(get_wrapper<5>(  boost::bind(&SomeStruct::another_method,local[2]) ));
    }
    

    It may not require Unique ID’s for each invocation, for instance because Functors may already have different types, or runtime scope of their usage do not overlap. But it is safer to use unique ID each time.

    Implementation:

    live demo

    #include <boost/optional.hpp>
    #include <boost/bind.hpp>
    #include <iostream>
    #include <ostream>
    using namespace std;
    
    template<unsigned ID,typename Functor>
    boost::optional<Functor> &get_local()
    {
        static boost::optional<Functor> local;
        return local;
    }
    
    template<unsigned ID,typename Functor>
    typename Functor::result_type wrapper()
    {
        return get_local<ID,Functor>().get()();
    }
    
    template<typename ReturnType>
    struct Func
    {
        typedef ReturnType (*type)();
    };
    
    template<unsigned ID,typename Functor>
    typename Func<typename Functor::result_type>::type get_wrapper(Functor f)
    {
        (get_local<ID,Functor>()) = f;
        return wrapper<ID,Functor>;
    }
    
    // ----------------------------------------------------------------------
    
    void test(void (*fptr)())
    {
        fptr();
    }
    
    struct SomeStruct
    {
        int data;
        void some_method()
        {
            cout << data << endl;
        }
        void another_method()
        {
            cout << -data << endl;
        }
    };
    
    int main()
    {
        SomeStruct local[] = { {11}, {22}, {33} };
    
        test(get_wrapper<0>(  boost::bind(&SomeStruct::some_method,local[0]) ));
        test(get_wrapper<1>(  boost::bind(&SomeStruct::another_method,local[0]) ));
    
        test(get_wrapper<2>(  boost::bind(&SomeStruct::some_method,local[1]) ));
        test(get_wrapper<3>(  boost::bind(&SomeStruct::another_method,local[1]) ));
    
        test(get_wrapper<4>(  boost::bind(&SomeStruct::some_method,local[2]) ));
        test(get_wrapper<5>(  boost::bind(&SomeStruct::another_method,local[2]) ));
    }
    

    P.S. Beaware of multi-thread access – in such cases you should use some kind of Thread-local storage data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a third party library which I finally have working within my MonoTouch
I have a third-party library (the interface to Xerox's Finite State tools) which come
I have a call to a third-party C++ library which I have put into
I have a third-party library that is sometimes throwing an exception. So I decided
I have a third party SDK in C# which has class names like this:
I have a solution file that requires a third party library (open source). The
I have a class library which is called through COM from a Third Party
I have written a class library in C# (mydll.dll) which accesses a third party
I have a third party library that is available for download in either 32-bit
I have a third party library (ZBar) which complains about the armv7 arch when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.