模板函数中的错误以获取来自C 输入函数的参数类型
Error in template function to get the parameter type from input function in C++?
我遇到了一些类似的问题,并找到了这个问题:是否可以找出lambda的参数类型和返回类型?
我在该问题的答案中使用代码:
#include <iostream>
#include <tuple>
template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())> {};
template <typename ClassType, typename ReturnType, typename... Args>
struct function_traits<ReturnType(ClassType::*)(Args...) const> {
enum { arity = sizeof...(Args) };
typedef ReturnType result_type;
template <size_t i>
struct arg {
typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
};
};
就我而言,(lambda)函数是用户定义的,因此我必须使用模板函数来基于用户定义的函数来执行某些操作,因此我在下面添加了一个代理函数:
template <class F>
void proxy(F func) {
typedef function_traits<decltype(func)> traits;
typename traits::result_type r;
traits::arg<0>::type p; // compile error
}
我在下面收到编译错误:
error: `::type` has not been declared
error: expected `;` before `p`
为什么可以在参数类型不能时汇编返回类型,我该如何使其工作?
使用TypeName和模板:
typename traits::template arg<0>::type p;
一行说明:
- 为什么要
typename
?因为::type
是一种因类型。 - 为什么
template
?因为arg<>
是一个因模板。
希望会有所帮助。
相关文章:
- 如果我更改函数输入,我的递归不起作用?
- 初始化与函数输入相关的全局数组
- 如何生成比用户使用 rand() 函数输入的数字大 5-10% 的数字?
- 将函数更改为具有设置函数输入变量的方法的类
- 如何避免将 const 和非 const 成员函数输入到模板中的代码重复
- 是否有一种方法可以避免标头文件中使用的constexpr函数输入全局范围,而无需额外的名称空间
- 迭代器作为C 中的函数输入
- 如何为作为雇员类的指针传入的构造函数输入数据
- 最小的函数输入和输出错误
- C++ 函数输入错误
- QComboBox 作为 QT 中的函数输入参数
- 函数输入之前的分割故障
- 如何在视觉工作室 2012年设置函数输入参数的颜色
- 函数输入变化
- 使用函数输入 1 个 int 和 1 个字符串数组并循环
- 将数组用于函数输入参数
- 如何在c++中将函数输入强制转换为智能指针
- 自定义检查器的覆盖率引用函数输入
- 为什么我不能使用两个scanf函数输入字母
- 犰狳cov函数输入