如何将函数指针声明指向模板函数,其返回类型取决于模板类
How to declare a function pointer to a template function whose return type is dependent on template class
标题听起来可能令人困惑,但这里有一些代码可以清除它,
template<class Act>
auto ActWrapper(Act && act, std::mutex mutex) -> decltype(act())
{
//....
return act();
}
如何编写函数指针以上函数?
没有模板函数指针,如果您正在寻找的话。如果您想将功能指针指向特定的实例化,则需要确切知道函数实例化的签名。
您总是可以用auto
作弊:
auto ptr = &ActWrapper<foo>;
如果您不能使用auto
,则需要知道返回类型:
return_t(*ptr)(foo&&, std::mutex) = &ActWrapper<foo>;
// or
decltype(ActWrapper(std::declval<foo>(), {}))(*ptr)(foo&&, std::mutex) = &ActWrapper<foo>;
如何编写函数指针以上函数?
您无法声明指向功能模板的功能指针。您只能声明函数指针指向功能。
如果Foo
对于ActWrapper
是有效的类型,则可以将功能指针声明为ActWrapper<Foo>
。当时您会知道函数的返回类型,并且可以使用它。
using returntype = <the return type of Foo::operator()>;
using FuncionPtrType = returntype (*)(Foo&&, std::mutex);
FuncionPtrType functionPtr = ActWrapper<Foo>;
正如其他人指出的那样,您不能将简单的函数指针指向整个函数集,而仅用于模板的特定实例化。
如果您需要一些东西,则可以传递到整个集合,而不是特定的功能,则需要将其包裹在lambda或带有operator()
的类中,然后将其转动。
相关文章:
- 检查函数返回类型是否与STL容器类型值相同
- 警告:在函数返回类型 [-Wignore 限定符] 时忽略类型限定符
- 在 c++ 中将函数返回类型指定为模板参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 如何避免模板函数返回类型重复?
- 从类型bankAccount的返回值到函数返回类型int没有可行的转换
- 为什么在某些情况下从函数返回类型中删除 cv 限定符?
- 模板类内模板类的函数返回类型
- 为什么函数返回类型中不允许参数推导?
- 函数返回类型之前的"define"
- C++推断要隐式调用的模板函数返回类型
- 具有不同模板参数的函数返回类型
- 当函数返回类型为父类时,如何返回子类的对象?
- C++:在原型中声明"auto"函数返回类型仍然会导致在扣除错误之前使用"auto&quo
- C++14 'auto'能够获取函数返回类型,我们还需要 std::result_of<> 吗?
- 函数返回类型中的模板类型推断
- 嵌套模板类返回类型在 C++ 中的头文件中函数返回类型的语法
- C++重写 void 函数返回类型会导致生成失败
- 从函数内部推断函数返回类型
- C++嵌套类函数返回类型和命名空间