简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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