.
가능성은 메서드와 개체에 대한 참조를 저장하는 래퍼를 구성하는 것입니다. 이 같은
뭔가 :
template<typename T, typename Fn>
struct MemberFunctionPointer {
MemberFunctionPointer(T* ref, Fn fn) : m_ref(ref),
m_method(fn) { }
template<typename... Args>
auto operator()(Args&&... args) {
return (m_ref->*m_method)(std::forward<Args...>(args)...);
}
T* m_ref = nullptr; // a reference (pointer) to the object instance
Fn m_method = nullptr; // a reference to the function method
};
참고 :이 그냥 처음이다. 보다 정교한 인터페이스를 추가해야합니다. 게다가 MemberFunctionPointer
객체를 생성하기위한 도우미 함수도 유용 할 수 있습니다.
단순히 function
대신 해당 종류의 개체를 전달할 수 있습니다.
struct Foo {
void bar() {
// something
}
};
int main(int argc, char *argv[]) {
Foo f;
MemberFunctionPointer<Foo, decltype(&Foo::bar)> method(&f, &Foo::bar);
method(); // call the method on the object f.
assert(&f == method.get_obj_reference());
return 0;
}
이것은 불가능합니다. 'std :: function'의 타겟을 검색 할 수는 있지만, 타겟 타입은 알 수없는 타입의 바인드 표현식입니다. 여러분은 이후 정보를 검색 할 인터페이스가 없습니다. –
@KerrekSB 대답 : – Quentin
@KerrekSB 예를 들어 std :: function :: target을 사용할 수 있습니까? –