【问题标题】:Wrapping C++ member function in C - Visual Studio 2013 template woes在 C 中包装 C++ 成员函数 - Visual Studio 2013 模板问题
【发布时间】:2015-07-07 08:05:42
【问题描述】:

我正在创建一个轻量级的跨平台插件框架,它在应用程序和插件之间使用 C 接口(通常,但并非总是用 C++ 编写)。

要帮助 C++ 应用程序和插件编写者,我面临的一个挑战是找到一种简单的方法来跨 C 接口公开 C++ 对象功能。我目前的解决方案感觉很简单,并使用模板来“构建”包装底层 C++ 成员函数的 C 签名函数,基于 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...);
  }
};

此模板的实例化在 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 接口的上下文中使用的模板代码:

#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::function 并免费获得所有这些东西有什么特别的原因吗?那是在 C++11 中出现的,所以希望 VS 2013 有支持?
  • @Useless:我已经编辑了问题,以提请注意示例应用程序中导致 Visual Studio 失败的代码行。
  • @Useless:如何使用 std::function 来实现我通过接受成员函数作为参数的模板实现的目标?我的目标是简洁地为应用程序或插件编写者创建的任何 C++ 对象建立一个 C 接口,尽可能少地重复样板代码。
  • 有趣。会想办法解决的。工作...

标签: c++ c plugins visual-studio-2013 interface


【解决方案1】:

前言: std::forward 在这里没用,因为Args... 是明确指定的。换句话说,实例化的 C 接口不再是模板。 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&lt;decltype(&amp;Derived::f), &amp;Derived::f, Base&gt;::invoke;

如果Derived::f 被重载,您必须像这样显式指定其类型:

... = c_interface_gen&lt;void(Derived::*)() const, &amp;Derived::f, Base&gt;::invoke;

注意,这里不需要为const成员函数指定const Base。您只需指定基本类型。模板会自动判断是否应该添加 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_interface_gen 只接受返回和参数类型作为参数,而不是函数本身。假设Greeter 有一个额外的函数void farewell(const char * recipient) const)。当你包装它时,模板类的 same 实例中的静态变量 s_fn_ 被覆盖,导致调用 greet_cb() 调用成员函数 farewell()!你对std::forward 的看法是对的——谢谢。
  • @duncan 你是对的。我悲惨地未能抓住这一点。会更新答案。
  • @duncan 已更新。希望它现在可以解决您的问题:-)
  • 谢谢灵曦。这太棒了——通过在模板类中使用模板化静态函数,它消除了对模板专业化的需要(这是 Visual Studio 的弱点),同时克服了需要在成员函数类型之前定义可变参数模板 (Args) 参数的问题。现在我明白了,它是如此简单(就像大多数好的解决方案一样)。我实际上比我的原始版本更喜欢你的解决方案——它更干净。明天我会接受你的回答,届时我可以奖励一些赏金:你真的让我摆脱困境!
  • @duncan 我很高兴我的解决方案有所帮助。另请查看更新的答案。我刚刚添加了一个替代和改进的解决方案供您考虑:-)
猜你喜欢
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多