سؤال

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. Secondly, after doing so, the issue still persists. This is a confirmed bug in the Visual Studio VC++ compiler

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top