如何检查函数参数的类型?

How to inspect the types of a function's parameters?

本文关键字:参数 类型 函数 何检查 检查      更新时间:2023-10-16

我有一个应用程序,我正在构建一个函数marshal_and_apply,它用一些参数调用其他函数(或函子)fmarshal_and_apply的工作是根据f参数的类型对参数应用一些特殊的封送处理。

如果f的一个参数是特殊类型marshal_me<T>,那么marshal_and_apply将通过一些专门分配的存储封送该参数,然后将其传递给f。为了执行分配,marshal_and_apply必须知道所有参数的存储要求,然后才能封送任何参数。


一些例子:

template<typename Function, typename... Args>
void marshal_and_apply(Function f, Args... args);
void func1(int x, int y);
void func2(marshal_me<int> x, int y);
void func3(marshal_me<int> x, marshal_me<int> y, marshal_me<int> z);
// this call would be equivalent to:
// func1(7,13)
marshal_and_apply(func1, 7, 13);
// this call would be equivalent to:
// auto storage = my_allocator(sizeof(int));
// auto x = marshal_me<int>(7, storage);
// func2(x, 13);
marshal_and_apply(func2, 7, 13);
// this call would be equivalent to:
// auto storage = my_allocator(sizeof(int) + sizeof(int) + sizeof(int));
// auto x = marshal_me<int>(7, storage);
// auto y = marshal_me<int>(13, storage + sizeof(int));
// auto z = marshal_me<int>(42, storage + sizeof(int) + sizeof(int));
// func3(x,y,z);
marshal_and_apply(func3, 7, 13, 42);

为了解决这个问题,marshal_and_apply似乎需要一种机制来检查f的参数类型。我怀疑这在一般情况下是不可能的,但可以识别一组特殊类型(在本例中为marshal_me<T>)中的一个是否可以转换为特定参数的类型。

我应该如何构建marshal_and_apply

也许是这样的:

template<typename Function, typename... Args>
void marshal_and_apply(Function f, Args &&... args)
{
    f(InspectAndModify<Args>::process(sizeof...(Args), std::forward<Args>(args))...);
}

现在定义:

template <typename T> struct InspectAndModify
{
    static T&& process(unsigned int N, T && t)
    {
        return std::forward<T>(t);
    }
};
template <typename T> struct InspectAndModify<marshal_me<T>>
{
     static T&& process(unsigned int N, marshal_me<T> && m)
     {
         /* ... */
     }
};

完全不同的东西:这种方法首先解析函数签名,然后对每对类型执行"静态转换",在这里可以插入marshal_me专用化:

template <typename T> struct marshal_me { marshal_me(T) { } };
template <typename To, typename From> struct static_transform;
template <typename T> struct static_transform<T, T>
{
  static T go(T t) { return t; }
};
template <typename T> struct static_transform<T, T&>
{
  static T go(T & t) { return t; }
};
template <typename T> struct static_transform<marshal_me<T>, T>
{
  static marshal_me<T> go(T && t) { return std::forward<T>(t); }
};
template<typename T, typename... Args>
struct marshal_impl
{
  template <typename ...Urgs>
  static T go(T(*f)(Urgs...), Args &&... args)
  {
    return f(static_transform<Urgs, Args>::go(std::forward<Args>(args))...);
  }
};
template<typename Function, typename... Args>
void marshal_and_apply(Function f, Args &&... args)
{
  marshal_impl<void, Args...>::go(static_cast<typename std::decay<Function>::type>(f),
                                  std::forward<Args>(args)...);
}