auto 无法推断?

auto fails to deduce?

struct test 
{
  void f() {};
};
test t1;

using memfun_t = void (test::*)();
memfun_t mf = &test::f;


auto a1 = &test::f; // OK 
auto a2 = t1.*mf;  // error 
auto a3 = &(t1.*mf); // still no luck

知道为什么不能推断出这个吗?我将不胜感激参考标准的答案。

编辑:

我找到了一个名为 __closure 的 RAD Studio 语言扩展,似乎可以解决这个问题。1 这是代码:

class base {
public:
    void func(int x) {
    };
};

typedef void(base::*pBaseMember)(int);

class derived : public base {
public:
    void new_func(int i) {
    };
};

int main(int argc, char * argv[]) {
    derived derivedObject;
    void(__closure * derivedClosure)(int);

    // Get a pointer to the ‘new_func’ member.
    // Note the closure is associated with the
    // particular object, ‘derivedObject’.
    derivedClosure = derivedObject.new_func;

    derivedClosure(3); // Call ‘new_func’ through the closure.
    return 0;
}

http://docwiki.embarcadero.com/RADStudio/Seattle/en/Closure

你不能使用

auto a2 = t1.*mf;  // error 

就像你不能使用:

auto a2 = t1.f;

t1.f 不是有效的表达式。无法通过 class 的实例获取指向成员函数的指针。不像 non-member 函数那样使用时会衰减为函数指针,成员函数不会衰减为成员函数指针。

来自 C++11 标准的相关文本:

Unary Operators

...

4 A pointer to member is only formed when an explicit & is used and its operand is a qualified-id not enclosed in parentheses. [ Note: that is, the expression &(qualified-id), where the qualified-id is enclosed in parentheses, does not form an expression of type “pointer to member.” Neither does qualified-id, because there is no implicit conversion from a qualified-id for a non-static member function to the type “pointer to member function” as there is from an lvalue of function type to the type “pointer to function” (4.3). Nor is &unqualified-id a pointer to member, even within the scope of the unqualified-id’s class. —end note ]