3

受到另一个问题的启发,我试图找到一种方法来推断给定用于调用该函数的实际参数的重载成员函数的类型。这是我到目前为止所拥有的:

#include <type_traits>

template<typename F, typename Arg>
struct mem_fun_type {
  // perform overload resolution here
  typedef decltype(std::declval<F>()(std::declval<Arg>())) result_type;
  typedef decltype(static_cast<result_type (F::*)(Arg)>(&F::operator())) type;
};

struct foo {};

struct takes_two
{
  void operator()(int);
  void operator()(foo);
};

struct take_one {
  void operator()(float);
};

int main()
{
  static_assert(std::is_same<mem_fun_type<take_one, float>::type, 
                             void (take_one::*)(float)>::value, "Zonk");
  static_assert(std::is_same<mem_fun_type<takes_two, double>::type, 
                             void (takes_two::*)(float)>::value, "Zonk");
  return 0;
}

只要模板参数 Arg 与实际类型匹配,static_cast 就会成功,但这只是重载解析(精确匹配)最简单的情况。是否可以在模板元编程中执行完整的重载解决过程?

这纯粹是假设性的,不适合实际使用。

4

1 回答 1

1

到目前为止,这是我最接近的:定义返回不同大小的表的函数,您的结果是sizeof(select(...))接收指向要匹配的函数的指针。为了确保即使给定类中不存在函数也能编译代码,您可以使用单独的 check has_function

重载决议的结果在select<has_function<T>::value, T>::value.

使用此代码,您甚至可以“解析”数据成员,而不仅仅是函数,这只是为选择函数制作正确参数的问题。

然而这里有一个缺陷——重载决议不是在函数参数上,而是在函数类型上。这意味着不会发生任何常见的参数类型转换。

  // Verify the name is valid
  template <typename T>
  struct has_function
  {
    struct F {int function;};
    struct D : T, F {};
    template <typename U, U> struct same_;
    template <typename C> static char(&select_(same_<int F::*, &C::function>*))[1];
    template <typename> static char(&select_(...))[2];
    enum {value = sizeof(select_<D>(0)) == 2};
  };

  // Values to report overload results
  enum type { none=1 , function_sz_size_t , function_sz , function_string };

  template <bool, typename R> struct select;

  template <typename R> struct select<false, R>
  {
    enum {value = none};
  };

  template <typename R> struct select<true, R>
  {
    // Define your overloads here, they don't have to be templates.
    template <typename Ret, typename Arg> static char(&select_(Ret (R::*)(const char*, Arg)))[function_sz_size_t];
    template <typename Ret, typename Arg> static char(&select_(Ret (R::*)(Arg)))[function_sz];
    template <typename Ret> static char(&select_(Ret (R::*)(std::string)))[function_string];
    template <typename Ret> static char(&select_(Ret (R::*)(std::string&&)))[function_string];
    template <typename Ret> static char(&select_(Ret (R::*)(const std::string&)))[function_string];
    static char(&select_(...))[none];
    enum {value = sizeof(select_(&R::function))};
  };
于 2012-01-20T02:26:23.433 回答