【发布时间】:2020-09-01 11:33:48
【问题描述】:
让我们考虑一种“调用”函数(这里称为“调用”),它有助于调用由模板参数传递的成员函数。 在这个函数中,我需要知道拥有成员函数的类的类型。有没有办法(最好在 c++14 中)这样做?
#include <functional>
template<typename F, typename... Args, std::enable_if_t<std::is_member_pointer<std::decay_t<F>>{}, int> = 0 >
constexpr decltype(auto) call(F&& f, Args&&... args) noexcept(noexcept(std::mem_fn(f)(std::forward<Args>(args)...)))
{
// Here we know that f is a member function, so it is of form : &some_class::some_function
// Is there a way here to infer the type some_class from f ? For exemple to instantiate a variable from it :
// imaginary c++ : class_of(f) var;
return std::mem_fn(f)(std::forward<Args>(args)...);
}
int main()
{
struct Foo { void bar() {} } foo;
call(&Foo::bar, &foo /*, args*/);
return 0;
}
【问题讨论】:
标签: c++ templates member-function-pointers