Search code examples
c++function-pointersboost-python

c++ pointers to overloaded functions


I'm trying to expose a overloaded function using boost::python. the function prototypes are:

#define FMS_lvl2_DLL_API __declspec(dllexport)
void FMS_lvl2_DLL_API write(const char *key, const char* data);
void FMS_lvl2_DLL_API write(string& key, const char* data);
void FMS_lvl2_DLL_API write(int key, const char *data);

I'v seen this answer: How do I specify a pointer to an overloaded function?
doing this:

BOOST_PYTHON_MODULE(python_bridge)
{
    class_<FMS_logic::logical_file, boost::noncopyable>("logical_file")
        .def("write", static_cast<void (*)(const char *, const char *)>( &FMS_logic::logical_file::write))
    ;
}

results with the following error:

error C2440: 'static_cast' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
      None of the functions with this name in scope match the target type

trying the following:

void (*f)(const char *, const char *) = &FMS_logic::logical_file::write;

results:

error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
          None of the functions with this name in scope match the target type

what's wrong and how to fix it?

EDIT I forgotten to mention a few things:

  • I'm using vs2010 pro on win-7
  • write is a member function of logical_file
  • FMS_logic is a namespace

Solution

  • Well the second attemp should work, if write is a pure function. From your code it seems you do have a memberfunction. Pointers to member-functions are ugly, you'd rather use a function object. However: you would have to post the whole code, it is not clear whether write is a member-function or not.

    Edit: if it is a member-function of FMS_logic::logical_file the syntax would be:

    void (FMS_logic::logical_file::*f)(const char *, const char *) = &FMS_logic::logical_file::write;
    

    This just applies for non-static member function, i.e. if a function is static or logical_file is just a namespace it is as you wrote it before.