仅在运行时给出签名时的函数指针调用

rit*_*ter 5 c++ function-pointers c++11

考虑以下情况:

您将获得指向函数的指针作为原始指针

void * function_pointer;
Run Code Online (Sandbox Code Playgroud)

并且要传递给函数的参数可用作联合类型的向量.

union Types {
  void   *ptr;
  float  *ptr_float;
  double *ptr_double;
  float  fl;
  int    in;
  double db;
  bool   bl;
};

std::vector<Types> arguments;
Run Code Online (Sandbox Code Playgroud)

因此,函数的签名仅在程序状态下可用(与编译时相同)

这个电话的推荐方式是什么(C++ 11)?

可以将参数向量更改为以下内容:

std::vector< std::pair<int,Types> > arguments;
Run Code Online (Sandbox Code Playgroud)

其中第一个元素将清楚地标识参数的类型.

从技术上讲,签名仅以第二种形式给出.因为只有第一种形式你才能分辨出签名是什么样的.

bee*_*boy 1

现有的库可以执行您所描述的操作,例如 C/Invoke:

http://www.nongnu.org/cinvoke/