在C-Visual Studio 2013模板中包装C++成员函数

dun*_*can 6 c c++ plugins interface visual-studio-2013

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

我帮助C++应用程序和插件编写器的挑战之一是找到一种在C接口上公开C++对象功能的简单方法.我现在的解决方案感觉很简单,并使用模板"构建"C-signature函数,根据这个伟大的stackoverflow问题和答案来包装底层的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...);
  }
};
Run Code Online (Sandbox Code Playgroud)

该模板的实例化在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'
Run Code Online (Sandbox Code Playgroud)

下面的独立示例代码显示了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;
}
Run Code Online (Sandbox Code Playgroud)

技术细节:

  • 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

Lin*_*gxi 2

前言: 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...);
  }
};
Run Code Online (Sandbox Code Playgroud)

这个版本有效。但它绝不是优雅的。主要问题在于使用该工具的语法冗长且不直观。

版本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>
{};
Run Code Online (Sandbox Code Playgroud)

显然,这个版本比前一个版本的代码更多。但优点是使用该工具的语法简单且直观。事实上,语法类似于您原来的设施。我只是添加了一些代码以使 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;
}
Run Code Online (Sandbox Code Playgroud)