我现在正在为一些 Windows 库函数编写包装器,因此我需要提取它们的原型(prototype)并使用修改后的原型(prototype)编写一个新的包装器。例如:函数
int recv(SOCKET, char*, int, int)
将由
包裹recv_wrapper(SOCKET, char*, int, int, THREADID)
为了避免错误,我写了一个包装器模板:
template <typename F> struct wrapper;
template <typename R, typename T1, typename T2, typename T3, typename T4>
struct wrapper<R(T1,T2,T3,T4)>
{
typedef R result_type;
typedef R (type)(T1,T2,T3,T4,THREADID);
};
然后下面的包装类型有效:
typedef int recv_t(SOCKET,char*,int,int);
typedef wrapper<recv_t>::type recv_wrapper_t;
但我想进一步使用 decltype 来自动获取 recv 的类型:
typedef decltype(recv) recv_t;
typedef wrapper<recv_t>::type recv_wrapper_t;
但这不起作用,我尝试了几种方法来修改它,但我总是从 VS2010 编译器中得到错误:
error C2027: use of undefined type 'wrapper<F>'
with
[
F=int (SOCKET,char *,int,int)
]
和
error C2146: syntax error : missing ';' before identifier 'recv_wrapper_t'
我对此感到非常沮丧,因为这里的 F
只不过是明确定义的 recv_t
:
typedef int recv_t(SOCKET,char*,int,int);
但是当用decltype
自动获取时,它就不再起作用了。
非常感谢您的考虑。
注意我已经按照 n.m. 的建议修复了一个拼写错误;但这仍然不起作用。
最佳答案
template <typename F> wrapper;
是无效的。你是说 template <typename F> struct wrapper;
?如果你解决了这个问题,明显的语法就可以工作了:
typedef wrapper<decltype(recv)>::type recv_wrapper_t;
关于c++ - 对函数的 decltype 感到沮丧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22514611/