std::bind使用中一个很蛋疼的问题——NULL的宏定义
来源:互联网 发布:知乎 驱动 微软认证 编辑:程序博客网 时间:2024/06/17 18:58
这几天写一个自用的定时器, 因为每个定时器都需要保存一个回调函数, 之前直接用的函数指针,后来想了下不靠谱。后来就用了std::function,用std::function的主要原因就是可以通过使用std::bind给函数绑定参数来实现对各种不同参数的函数调用, 主要是对于成员函数的引用。
因为之前没用过std::bind, 所以特意去网上看了下, 可以绑定全局函数, 静态成员函数,普通成员函数等。
函数申明和绑定方法如下:
void NetWorkThread::OnTimer( unsigned int nType, void* vLParam ); //回调函数的申明, 因为多个定时器都用到了这个成员函数,所以第一个参数带了类型, 第二个参数考虑到可能需要给定时器传参数,所以用了个void*指针类型
std::function<void(void)>; //定时器回调函数参数
auto tFunc = std::bind( &NetWorkThread::OnTimer, this, nType, NULL ); //绑定
::SetTimer( false, nType, tTime, tFunc ); //调用
刚开始我就是这样写的, 看上去没问题吧, 我也是这样认为的。可是编译直接提示错误了:
1>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(1152): error C2664: “_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()<NetWorkThread*>(const _Wrapper &,_V0_t,_V1_t) const”: 不能将参数 3 从“int”转换为“void *”
1> with
1> [
1> _Rx=void,
1> _Pmf_t=void (__thiscall NetWorkThread::* )(unsigned int,void *),
1> _Farg0=NetWorkThread,
1> _V0_t=unsigned int,
1> _V1_t=void *,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil,
1> _Wrapper=NetWorkThread *
1> ]
1> 从整型转换为指针类型要求 reinterpret_cast、C 样式转换或函数样式转换
1> C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\functional(1152): 编译类 模板 成员函数“void std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>::operator ()(void)”时
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=std::_Pmf_wrap<void (__thiscall NetWorkThread::* )(unsigned int,void *),void,NetWorkThread,unsigned int,void *,std::_Nil,std::_Nil,std::_Nil,std::_Nil,std::_Nil>,
1> _V0_t=NetWorkThread *const ,
1> _V1_t=int,
1> _V2_t=int,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil
1> ]
一开始一只没看下边的错误提示中参数的具体类型,只看了第三个参数不能从int转换为void* 我一看就奇怪了, 第三个参数是nType,刚好对应OnTimer的第一个参数 unsigned int 型。没错啊, 怎么会说无法转换为void*型呢, 看了很久一直没搞定, 纠结。
如果我仔细点的话就可以看出, 是_V2_t这个参数类型错了, 我写的是NULL, 但编译器识别成了int型,这样当然无法转换为void*型,想到这我就换成nullptr试了下, 立马就好了。
后来看了下NULL的定义 在WinDef.h里面,被宏定义为0,而不是(void*)0
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
- std::bind使用中一个很蛋疼的问题——NULL的宏定义
- std::bind的使用
- std::bind()的使用
- C++11中std::bind的使用
- std::bind的一个例子
- std::bind,std::cref的简单使用
- std::function和std::bind的使用?
- std::tr1::bind的使用
- C++中std::tr1::function和bind 组件的使用
- C++中std::tr1::function和bind 组件的使用
- C++中std::tr1::function和bind 组件的使用
- C++中std::tr1::function和bind 组件的使用
- C++中std::tr1::function和bind 组件的使用
- C++中std::tr1::function和bind 组件的使用
- C++11 中std::function和std::bind的用法
- C++11 中std::function和std::bind的用法
- 使用std::function和std::bind写的观察者模式
- std::bind绑定器、std::function、extern C的使用
- IOS开发入门之创建项目
- 黑马程序员_java Io详解
- 理解xib(nib)、File’s Owner、First Responder、outlet、action等概念
- fnd_request.submit_request 的用法
- Leetcode Add Two Numbers
- std::bind使用中一个很蛋疼的问题——NULL的宏定义
- 扩充 staticContent 节点,让 IIS 看的懂 HTML5 Video 与 WebFont
- 关于json_encode中文为null的情况
- 关于DBN的几点思考
- Step for creating new Requirement Routine (Output Control) for output type
- C++并发实战10:保护共享数据的可选机制
- js setTimeout()函数 [倒计时用]
- linux进程任务状态转换图
- 算法之平衡树 - 红黑树(JQuery+Js+Canvas版本的,帮助大家理解)