CPP函数类型转换

1. 类成员函数转成std::function及lambda

class MyClass{
  pub:
    int Func(int a) {};
};
int main(void) {
    MyClass my_class;
    std::function<int(int a)> func_ = std::bind(&MyClass::Func, &my_class, std::placeholders::_1);

    auto lambda_func_ = [&my_class](int a) -> int {
        return my_class.Func(a);
    }
}

 2. lambda转成std::function,参考https://www.cnblogs.com/qicosmos/p/4772328.html,做类型萃取,链接还讲了any class,有时间要看看。

// g++ lambda_to_func.cpp -std=c++11 -Werror
#include <cxxabi.h>

#include <cstdio>
#include <functional>
#include <iostream>

template <typename T>
struct function_traits;

//普通函数
template <typename Ret, typename... Args>
struct function_traits<Ret(Args...)> {
 public:
  // enum { arity = sizeof...(Args) };
  static const size_t arity = sizeof...(Args);
  typedef Ret function_type(Args...);
  // typedef Ret return_type;
  using return_type = Ret;
  using stl_function_type = std::function<function_type>;
  typedef Ret (*pointer)(Args...);

  template <size_t I>
  struct args {
    static_assert(I < arity,
                  "index is out of range, index must less than sizeof Args");
    using type = typename std::tuple_element<I, std::tuple<Args...>>::type;
  };
};

//函数指针
template <typename Ret, typename... Args>
struct function_traits<Ret (*)(Args...)> : function_traits<Ret(Args...)> {};

// std::function
template <typename Ret, typename... Args>
struct function_traits<std::function<Ret(Args...)>>
    : function_traits<Ret(Args...)> {};

// member function
#define FUNCTION_TRAITS(...)                                             
  template <typename ReturnType, typename ClassType, typename... Args>   
  struct function_traits<ReturnType (ClassType::*)(Args...) __VA_ARGS__> 
      : function_traits<ReturnType(Args...)> {};

FUNCTION_TRAITS()
FUNCTION_TRAITS(const)
FUNCTION_TRAITS(volatile)
FUNCTION_TRAITS(const volatile)

//函数对象
template <typename Callable>
struct function_traits : function_traits<decltype(&Callable::operator())> {};

template <typename T>
void PrintType() {
  std::cout << abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, nullptr)
            << std::endl;
}
int main(void) {
  int my_int = 10;
  std::cout << abi::__cxa_demangle(typeid(my_int).name(), nullptr, nullptr,
                                   nullptr)
            << std::endl;

  std::function<int(int)> f = [](int a) { return a; };

  //打印函数类型
  PrintType<function_traits<
      std::function<int(int)>>::function_type>();  //将输出int __cdecl(int)

  //打印函数的第一个参数类型
  PrintType<
      function_traits<std::function<int(int)>>::args<0>::type>();  //将输出int

  //打印函数的返回类型
  PrintType<function_traits<decltype(f)>::return_type>();  //将输出int

  //打印函数指针类型
  PrintType<function_traits<decltype(f)>::pointer>();  //将输出int

  int a = 2;
  int b = 3;
  auto lambda_add = [a, b](int c) -> int { return a + b + c; };

  auto my_add =
      new std::function<function_traits<decltype(lambda_add)>::return_type(
          function_traits<decltype(lambda_add)>::args<0>::type)>(lambda_add);
  int ret1 = (*my_add)(5);
  int ret2 = lambda_add(5);
  printf("ret %d %d 
", ret1, ret2);

  return 0;
}
原文地址:https://www.cnblogs.com/zengjianrong/p/14364337.html