【发布时间】:2012-11-30 12:34:07
【问题描述】:
我使用以下代码来检测给定函数的长参数。
所以,给定:
int f(int *) { return 0; }
我要提取int *。
这是我的尝试:
template<class T, class U> struct SingleArg {
typedef U MyArg;
};
template<class T, class U> SingleArg<T, U> fT(T (*p)(U));
int main() {
std::result_of<decltype(fT(f))>::type::MyArg t;
}
但这不起作用,gcc 4.6 给出错误
> error: std::result_of<SingleArg<int, int*> >::type has not been
> declared
所以,我有两个问题:
a) 上面的代码有什么问题?
b) 是否有可能以任何其他方式/方式做到这一点?
【问题讨论】:
-
“函数返回类型”,“检测长参数”。你真正想要什么?
-
是的。想检测函数参数类型。将修改帖子标题
-
decltype(fT(f))::MyArg?为什么还要使用result_of?它做了一些完全不同的事情(并且有不同的使用语法)。 -
嗯,编译器错误。
template<class T> using Alias = T; Alias<decltype(fT(f))>::MyArg。如果 GCC 4.6 没有使用别名,请使用template<class T> struct identity{ typedef T type; },然后使用identity<decltype(fT(f))>::type::MyArg。 -
出于兴趣,检测类型的目的是什么?如果有办法在没有这个的情况下实现你的更大目标,我会推荐它。
标签: c++ c++11 decltype result-of