c++ 传入函数指针解析需要的参数 C++根据传入的函数指针来解析需要的参数(推荐)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

c++ 传入函数指针解析需要的参数 C++根据传入的函数指针来解析需要的参数(推荐)

月落无影   2021-03-28 我要评论
想了解C++根据传入的函数指针来解析需要的参数(推荐)的相关内容吗,月落无影在本文为您仔细讲解c++ 传入函数指针解析需要的参数的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:c++,传入函数指针,c,函数指针,参数,下面大家一起来学习吧。

C++可以根据传入的函数指针,获取自己需要的参数类型,然后根据参数源中获取需要的参数,这里我用tuple作为演示,不过,只要可以根据序号,或者顺序方式等获取实参,都可以使用类似的方式实现:

先给出一个辅助函数:

/** 获取第N个类型
*/
template <typename... Cases>
struct select
{
};
template <typename T, typename... Cases>
struct select<T, Cases...> : public select<Cases...>
{
  using ThisType = T;
  using Base = select<Cases...>;
};

下面给出实际的实现函数:

#include <functional>
#include "vs-help.h"

class TupleFunc
{
public:
  TupleFunc() { }

  // 函数的实际构造过程
  template <typename Ret, typename... Args, typename ParamsSource>
  void makeFuncAndParams(Ret(*func)(Args...), ParamsSource& paramSource)
  {
    makeFuncAndParamsImpl<0>(func, select<Args...>(), paramSource);
  }

  // 实际调用
  void invoke() 
  {
    m_func();
  }

private:
  // 实际调用初始化
  template <size_t idx, typename Func, typename Select, typename ParamsSource, typename... Params>
  void makeFuncAndParamsImpl(Func&& func, Select, ParamsSource& paramSource, Params&&...args)
  {
    typename Select::ThisType param = std::get<idx>(paramSource);
    makeFuncAndParamsImpl<idx + 1>(func, Select::Base(), paramSource, std::forward<Params>(args)..., std::move(param));
  }
    
  // 结束调用
  template <size_t idx, typename Func, typename ParamSource, typename... Params>
  void makeFuncAndParamsImpl(Func&& func, select<>, ParamSource& paramSource, Params&&... args)
  {
    m_func = [func, args...]() { func(args...); };
  }
private:
  std::function<void()> m_func;
};

下面是测试用例:

void print(int x, std::string y)
{
  std::cout << "x: " << x << std::endl;
  std::cout << "y: " << y << std::endl;
}
int main()
{
  std::tuple<int, std::string, std::string> p = { 12, "job", "China" };
  TupleFunc func;
  func.makeFuncAndParams(&print, p);
  func.invoke();
  return 0;
}

通过使用lambda表达式,我们可以很方便的构建一个我们调用时需要的函数,而模板的存在,可以使我们在构建一个lambda表达式的时候,可以动态化,使某些情况下,更加灵活的构建各类处理函数map等。上面只是简单的演示,具体场景下,需要进行一定的修改。

总结

以上所述是小编给大家介绍的C++根据传入的函数指针来解析需要的参数,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们