简体   繁体   English

指向成员函数指针的模板参数推导

[英]Template Argument Deduction for Pointer to Member Function

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. 我只是想知道为什么编译器可以为该类而不是该函数推断成员函数的类型。

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. 首先,调用约定是错误的,并且PMF中应该有一个__cdecl Secondly, after doing so, the issue still persists. 其次,这样做之后,问题仍然存在。 This is a confirmed bug in the Visual Studio VC++ compiler 这是Visual Studio VC ++编译器中的一个已确认错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM