我知道还有很多其他类似的问题,但我所看到的那些问题似乎都不适用于我正在做的事情。 我所拥有的主旨是:
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
更多推荐
发布评论