0
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
任何想法爲什麼這不能被推斷?我希望引用標準的答案。汽車未能推斷出?
編輯:
我發現,似乎是解決這一問題的RAD Studio語言的擴展名爲__closure。 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
那麼這就是現在的問題,不是嗎?爲什麼它不能推導出成員函數指針? – Lenz
@倫茨,如果'mf'不是帖子的一部分,那將是個問題。 –
@Lenz:它不能推導出一個成員函數指針,因爲只能推導出有效的表達式,並且它不是一個有效的表達式。爲什麼它不是一個有效的表達?因爲這些是C++成員函數和成員指針的規則。 –