我正在尝试创建一个类模板,其构造函数可以将任何类型的函数作为参数,也就是说,它需要一个函数指针(可以是成员函数指针)和相应的函数参数。此外,应该有一个static_assert
检查函数返回类型(取自函数指针)是否与类模板参数类型匹配。因此,代码应如下所示:
template <class ReturnType>
struct Bar
{
template <class RetType, class ... ParamType>
Bar<ReturnType>(RetType (* func)(ParamType ...), ParamType && ... args) :
package_(std::bind(func, std::forward<ParamType>(args) ...)),
function_([this] { package_(); }),
future_(package_.get_future())
{
static_assert(std::is_same<ReturnType, RetType>::value,
"Type mismatch between class parameter type and constructor parameter type");
}
template <class RetType, class ObjType, class ... ParamType>
Bar<ReturnType>(RetType (ObjType::* func)(ParamType ...), ObjType * obj, ParamType && ... args) :
package_(std::bind(func, obj, std::forward<ParamType>(args) ...)),
function_([this] { package_(); }),
future_(package_.get_future())
{
static_assert(std::is_same<ReturnType, RetType>::value,
"Type mismatch between class parameter type and constructor parameter type");
}
std::packaged_task<ReturnType()> package_;
std::function<void()> function_;
std::future<ReturnType> future_;
};
这个想法是代码针对这些情况进行编译,并允许Bar::function_
(通过函数调用运算符)无错误地调用:
struct Foo
{
int foo(int i) {
return i;
}
int foo() {
return 1;
}
};
int foo(int i)
{
return i;
}
int foo()
{
return 1;
}
int main()
{
Foo f = Foo();
Bar<int> b1(&Foo::foo, &f, 1);
Bar<int> b2(&Foo::foo, &f);
Bar<int> b3(foo, 1);
Bar<int> b4(foo);
return 0;
}
不幸的是,我在模板元编程方面的经验几乎为零,即使我在 SO 中遇到了几个问题,并尝试了几种解决问题的方法,例如对构造函数使用更通用的方法
template <class RetType, class ... ParamType>
Bar<ReturnType>(RetType func, ParamType && ... args)
并结合它type_traits
来确定返回类型),我还没有找到一种方法来完成这项工作。我可以对允许此功能的构造函数进行哪些更改?
编辑:
max66 的回答解决了我原来的问题,但是,出现了一个新问题,我在上一个问题中没有考虑过。我还希望能够将变量传递给构造函数,如下所示:
int main()
{
Foo f = Foo();
int i = 1;
Bar<int> b1(&Foo::foo, &f, i); // Error
Bar<int> b2(&Foo::foo, &f, 1); // Ok
Bar<int> b3(&Foo::foo, &f); // Ok
Bar<int> b4(foo, i); // Error
Bar<int> b5(foo, 1); // Ok
Bar<int> b6(foo); // Ok
return 0;
}
但是,实际上,在标有 的情况下会出现编译器错误Error
。我猜这是因为func
构造函数中的参数用于确定其类型(在 and 的情况下与实际的 s不ParamType
匹配),但我不知道如何解决这个问题......ParamType
b1
b4