我正在创建一个轻量级跨平台插件框架,它在应用程序和插件之间使用 C 接口(interface)(通常,但不总是,用 C++ 编写)。
我在帮助 C++ 应用程序和插件编写者方面面临的挑战之一是找到一种简单的方法来跨 C 接口(interface)公开 C++ 对象功能。我目前的解决方案感觉很简单,并使用模板“构建”基于 this great stackoverflow question and answer 包装底层 C++ 成员函数的 C 签名函数。
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...);
}
};
此模板的实例在 linux (gcc) 和 mac (clang) 下编译和运行良好,但在 Visual Studio 2013 中编译失败:
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'
下面的独立示例代码显示了 Visual Studio 失败的行(在 Greeter
类定义中)。我希望有人可以:
- 提供一种可以安抚 Visual Studio 的解决方法,或者
- 提出实现类似目标的替代策略
下面的独立代码演示了在相当冗长的 Hello world
应用程序中使用 C++ 类实现 C 接口(interface)的上下文中使用的模板代码:
#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;
}
技术细节:
- Linux 开发:Centos 7、g++ (GCC) 4.8.3 20140911
- Mac 开发:Apple LLVM 版本 6.1.0 (clang-602.0.49)
- Windows 开发:Visual Studio Express 2013 Update 5 CTP
最佳答案
前言: std::forward
在这里没用,因为 Args...
被明确指定。换句话说,实例化的 C 接口(interface)不再是模板。 std::forward
在非模板代码中没有用。为此,std::forward
未在以下解决方案中使用。
版本 1:
template <typename Base, typename Derived, typename R, typename... Args>
struct c_interface_gen {
template <R(Derived::*mem_fn)(Args...)> inline
static R invoke(Base* pb, Args... args) {
return (static_cast<Derived*>(pb)->*mem_fn)(args...);
}
template <R(Derived::*mem_fn)(Args...) const> inline
static R invoke(const Base* pb, Args... args) {
return (static_cast<const Derived*>(pb)->*mem_fn)(args...);
}
};
这个版本有效。但这绝不是优雅的。主要问题在于使用该工具的语法冗长且不直观。
版本 2:
template <typename Sig>
struct mem_fn_sig;
template <typename R, typename D, typename... Args>
struct mem_fn_sig<R(D::*)(Args...)> {
template <R(D::*mem_fn)(Args...)>
struct mem_fn_inst {
template <typename Base>
struct base {
inline static R invoke(Base* pb, Args... args) {
return (static_cast<D*>(pb)->*mem_fn)(args...);
}
};
};
};
template <typename R, typename D, typename... Args>
struct mem_fn_sig<R(D::*)(Args...) const> {
template <R(D::*mem_fn)(Args...) const>
struct mem_fn_inst {
template <typename Base>
struct base {
inline static R invoke(const Base* pb, Args... args) {
return (static_cast<const D*>(pb)->*mem_fn)(args...);
}
};
};
};
template <typename Sig, Sig inst, typename Base>
struct c_interface_gen:
mem_fn_sig<Sig>:: template mem_fn_inst<inst>:: template base<Base>
{};
很明显,这个版本的代码比上一个版本多。但好处是使用该工具的语法简单直观。事实上,语法类似于您的原始工具。我刚刚添加了一些代码来简化 MSVC 的编译过程。
通常,您将像这样使用该工具:
... = c_interface_gen<decltype(&Derived::f), &Derived::f, Base>::invoke;
如果Derived::f
重载了,你必须像这样明确指定它的类型:
... = c_interface_gen<void(Derived::*)() const, &Derived::f, Base>::invoke;
请注意,无需指定 const Base
这里是const
成员函数。您只需指定基本类型。模板将自动计算出是否 const
是否加修饰符。
以下是您使用第二个版本的示例代码:
#include <iostream>
template <typename Sig>
struct mem_fn_sig;
template <typename R, typename D, typename... Args>
struct mem_fn_sig<R(D::*)(Args...)> {
template <R(D::*mem_fn)(Args...)>
struct mem_fn_inst {
template <typename Base>
struct base {
inline static R invoke(Base* pb, Args... args) {
return (static_cast<D*>(pb)->*mem_fn)(args...);
}
};
};
};
template <typename R, typename D, typename... Args>
struct mem_fn_sig<R(D::*)(Args...) const> {
template <R(D::*mem_fn)(Args...) const>
struct mem_fn_inst {
template <typename Base>
struct base {
inline static R invoke(const Base* pb, Args... args) {
return (static_cast<const D*>(pb)->*mem_fn)(args...);
}
};
};
};
template <typename Sig, Sig inst, typename Base>
struct c_interface_gen:
mem_fn_sig<Sig>:: template mem_fn_inst<inst>:: template base<Base>
{};
//
// 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
//
// 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 = c_interface_gen<decltype(&Greeter::greet), &Greeter::greet, greeter_c>::invoke;
}
// 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(static_cast<const greeter_c *>(&a));
return 0;
}
关于c++ - 在 C 中包装 C++ 成员函数 - Visual Studio 2013 模板问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29919987/