【发布时间】:2014-01-22 16:32:46
【问题描述】:
我遇到了一些类似的问题,发现这个问题:Is it possible to figure out the parameter type and return type of a 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`
为什么返回类型可以编译而参数类型不能编译,我该如何让它工作?
【问题讨论】:
标签: c++ templates c++11 lambda typetraits