I'm creating a lightweight cross-platform plugin framework which uses C interfaces between the application and the plugin (both typically, but not always, written in C++).
One of my challenges to aid C++ application and plugin writers is to find a simple way to expose C++ object functionality across a C interface. My present solution feels simple and uses templates to "build" C-signature functions that wrap underlying C++ member functions, based on this great stackoverflow question and answer
template <typename Tc, typename F, F>
struct MemberFuncWrapper;
template <typename Tc, // C interface structure tag
typename T, // C++ class, derived from Tc
typename R, // C++ member function return type
typename ...Args, // C++ member function argument types
R (T::*f)(Args...) const> // C++ member function
struct MemberFuncWrapper<Tc, R (T::*)(Args...) const, f> {
static R call(const Tc * tc, Args... args) {
const T * t = static_cast<const T *>(tc);
return ((*t).*f)(args...);
}
};
Instantiation of this template compiles and runs well under linux (gcc) and mac (clang) but compilation in Visual Studio 2013 fails with:
error C2440: 'specialization' : cannot convert from 'overloaded-function' to 'void (__cdecl Greeter::* )(void) const'
error C2973: 'MemberFuncWrapper<Tc,R(__cdecl T::* )(Args...) const,f>' : invalid template argument 'overloaded-function'
The standalone example code below shows the line where Visual Studio fails (in the Greeter
class definition). I'm hoping someone can either:
- Offer a workaround that will appease Visual Studio, or
- Suggest an alternative strategy to achieve similar ends
Here's standalone code that demonstrates the template code used in the context of implementing a C interface using a C++ class in a rather verbose Hello world
application:
#include <iostream>
#include <utility>
//
// C interface and function(s) typically defined elsewhere
//
#ifdef __cplusplus
extern "C" {
#endif
// The C interface implemented by a 'greeter'
struct greeter_c {
void(*greet_cb)(const struct greeter_c * greeter,
const char * recipient);
};
// Some C function that makes use of a greeter
void broadcast(const struct greeter_c * greeter) {
greeter->greet_cb(greeter, "world");
}
#ifdef __cplusplus
} // extern "C"
#endif
//
// Template magic that envelopes a C++ member
// function call in a C-signature function
//
template <typename Tc, typename F, F>
struct MemberFuncWrapper;
template <typename Tc, // C interface structure tag
typename T, // C++ class, derived from Tc
typename R, // C++ member function return type
typename ...Args, // C++ member function argument types
R (T::*f)(Args...) const> // C++ member function
struct MemberFuncWrapper<Tc, R (T::*)(Args...) const, f> {
static R call(const Tc * tc, Args... args) {
// Cast C structure to C++ object
const T * t = static_cast<const T *>(tc);
// Details such as catching/handling exceptions omitted.
// Call C++ member function
return ((*t).*f)(args...);
}
};
// Repeat of the above for non-const member functions omitted
//
// A C++ class that implements the C 'greeter' interface
//
class Greeter : public greeter_c {
public:
// Constructor
Greeter(const char * greeting) : m_greeting(greeting) {
// Set up C interface callback by wrapping member function
// !! The following line causes the Visual Studio compilation error !!
greet_cb = MemberFuncWrapper<greeter_c,
void (Greeter::*)(const char *) const,
&Greeter::greet>::call;
}
// C++ member function that 'does' the greeting
void greet(const char * recipient) const {
std::cout << m_greeting << " " << recipient << std::endl;
}
private:
const char * m_greeting;
};
// An application that greets using a Greeter's C interface
int main(int argc, char * argv[]) {
// Create C++ object that implements C interface
Greeter a("Hello");
// Greet using Greeter's C interface
broadcast(&a);
return 0;
}
Technical details:
- Linux development: Centos 7, g++ (GCC) 4.8.3 20140911
- Mac development: Apple LLVM version 6.1.0 (clang-602.0.49)
- Windows development: Visual Studio Express 2013 Update 5 CTP