如何使用模板专门化来查找成员函数参数类型等?

发布于 2024-12-27 17:17:42 字数 1556 浏览 4 评论 0原文

我确信我以前见过这种描述,但现在我一辈子都找不到它。

给定一个具有某种形式的成员函数的类,例如:

int Foo::Bar(char, double)

如何使用模板和各种专业化来推导构成类型,例如:

template<typename Sig>
struct Types;

// specialisation for member function with 1 arg
template<typename RetType, typename ClassType, etc...>
struct Types<RetType (ClassType::*MemFunc)(Arg0)>
{
    typedef RetType return_type;
    typedef ClassType class_type;
    typedef MemFunc mem_func;
    typedef Arg0 argument_0;
    etc...
};

// specialisation for member function with 2 args
template<typename RetType, typename ClassType, etc...>
struct Types<RetType (ClassType::*MemFunc)(Arg0, Arg1)>
{
    typedef RetType return_type;
    typedef ClassType class_type;
    typedef MemFunc mem_func;
    typedef Arg0 argument_0;
    typedef Arg0 argument_1;
    etc...
};

这样当我使用上述成员函数实例化类型时,例如:

Types<&Foo::Bar>

它解析为正确的专业化,并声明相关的 typedef?

编辑:

我正在使用快速委托,并将回调静态绑定到成员函数。

我有以下模型,我相信它会静态绑定到成员函数:

#include <iostream>

template<class class_t, void (class_t::*mem_func_t)()>
struct cb
{
    cb( class_t *obj_ )
        : _obj(obj_)
    { }

    void operator()()
    {
      (_obj->*mem_func_t)();
    }

    class_t *_obj;
};

struct app
{
  void cb()
  {
    std::cout << "hello world\n";
  }
};

int main()
{
  typedef cb < app, &app::cb > app_cb;

  app* foo = new app;
  app_cb f ( foo );
  f();
}

但是 - 如何以上述方式将其作为专业化?

I'm sure I've seen this described before but can't for the life of me find it now.

Given a class with a member function of some form, eg:

int Foo::Bar(char, double)

How can I use a template and various specialisations to deduce the constituent types, eg:

template<typename Sig>
struct Types;

// specialisation for member function with 1 arg
template<typename RetType, typename ClassType, etc...>
struct Types<RetType (ClassType::*MemFunc)(Arg0)>
{
    typedef RetType return_type;
    typedef ClassType class_type;
    typedef MemFunc mem_func;
    typedef Arg0 argument_0;
    etc...
};

// specialisation for member function with 2 args
template<typename RetType, typename ClassType, etc...>
struct Types<RetType (ClassType::*MemFunc)(Arg0, Arg1)>
{
    typedef RetType return_type;
    typedef ClassType class_type;
    typedef MemFunc mem_func;
    typedef Arg0 argument_0;
    typedef Arg0 argument_1;
    etc...
};

Such that when I instantiate Types with my above member function, eg:

Types<&Foo::Bar>

it resolves to the correct specialisation, and will declare the relevant typedefs?

Edit:

I'm playing around with fast-delegates with the callback statically bound to a member function.

I have the following mockup which I believe does statically bind to the member function:

#include <iostream>

template<class class_t, void (class_t::*mem_func_t)()>
struct cb
{
    cb( class_t *obj_ )
        : _obj(obj_)
    { }

    void operator()()
    {
      (_obj->*mem_func_t)();
    }

    class_t *_obj;
};

struct app
{
  void cb()
  {
    std::cout << "hello world\n";
  }
};

int main()
{
  typedef cb < app, &app::cb > app_cb;

  app* foo = new app;
  app_cb f ( foo );
  f();
}

However - how to get this as a specialisation in the manner above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨巷深深 2025-01-03 17:17:42

您几乎已经得到它,除了额外的 MemFunc,它不是类型的一部分。

template<typename RetType, typename ClassType, typename Arg0>
struct Types<RetType (ClassType::*)(Arg0)>   // <-- no MemType
{
    typedef RetType return_type;
    typedef ClassType class_type;
//  typedef MemFunc mem_func;     // <-- remove this line
    typedef Arg0 argument_0;
};

然而,您不能使用,

Types<&Foo::Bar>

因为 Foo::Bar 是一个成员函数指针,而不是它的类型。您需要一些编译器扩展来获取 C++03 中的类型,例如 gcc 中的 typeofBoost.Typeof

Types<typeof(&Foo::Bar)>

或升级到 C++11 并使用标准 decltype

Types<decltype(&Foo::Bar)>

You've almost got it, except that extra MemFunc, which is not part of the type.

template<typename RetType, typename ClassType, typename Arg0>
struct Types<RetType (ClassType::*)(Arg0)>   // <-- no MemType
{
    typedef RetType return_type;
    typedef ClassType class_type;
//  typedef MemFunc mem_func;     // <-- remove this line
    typedef Arg0 argument_0;
};

Nevertheless, you cannot use

Types<&Foo::Bar>

because Foo::Bar is a member function pointer, not the type of it. You'll need some compiler extensions to get the type in C++03, e.g. typeof in gcc or Boost.Typeof:

Types<typeof(&Foo::Bar)>

or upgrade to C++11 and use the standard decltype:

Types<decltype(&Foo::Bar)>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文