指向成员函数的模板参数推导(Template Argument Deduction for Pointer to Member Function)

编程入门 行业动态 更新时间:2024-10-25 19:37:00
指向成员函数的模板参数推导(Template Argument Deduction for Pointer to Member Function)

我知道还有很多其他类似的问题,但我所看到的那些问题似乎都不适用于我正在做的事情。 我所拥有的主旨是:

template <typename T> void CallFn(T *p, void (T::*pfn)(void)) { (p->*pfn)(); }

叫做使用:

class Foo { public: void Bar(void); } ... Foo *p = ... CallFn(p, &Foo::Bar);

但这给了我一个错误,说编译器不能推导出指向成员函数的指针的模板参数。 如果我改为使用这样的结构:

template <typename T> class Wrapper { public: void operator()(T *p, void (T::*pfn)(void)) { (p->*pfn)(); } }; ... Foo *p = ... Wrapper<Foo> x; x(p, &Foo::Bar);

它有效,但语法更加可怕。 我只是想知道为什么编译器可以推断出类的成员函数的类型,但不是函数。

I know there are a lot of other similar questions, but none of the ones I have looked at seem to apply to what I'm doing. The jist of what I have is:

template <typename T> void CallFn(T *p, void (T::*pfn)(void)) { (p->*pfn)(); }

called using:

class Foo { public: void Bar(void); } ... Foo *p = ... CallFn(p, &Foo::Bar);

but that gives me an error saying the complier couldn't deduce template arguments for the pointer to member function. If I instead use a struct like so:

template <typename T> class Wrapper { public: void operator()(T *p, void (T::*pfn)(void)) { (p->*pfn)(); } }; ... Foo *p = ... Wrapper<Foo> x; x(p, &Foo::Bar);

it works, but the syntax is much more horrible. I was just wondering why the compiler could deduce the type of the member function for the class, but not for the function.

最满意答案

所以,这里似乎有一些事情发生。 首先,调用约定是错误的,PMF中应该有一个__cdecl 。 其次,在这样做之后,问题仍然存在。 这是Visual Studio VC ++编译器中已确认的错误

So, there appears to be a couple of things going on here. First, the calling convention is wrong, and there should be a __cdecl in the PMF. Secondly, after doing so, the issue still persists. This is a confirmed bug in the Visual Studio VC++ compiler

更多推荐

本文发布于:2023-08-06 21:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   模板   成员   参数   Template

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!