[转] std::_Copy_impl
来源:互联网 发布:天天向上网络 编辑:程序博客网 时间:2024/06/05 20:11
在C++ PRIMER中模拟实现vector容器所给的代码中,如果你用的是vs 2012的话,那么你会发现根本就不能通过编译,这时应该怎么办呢?
alloc.construct(newelements+i , elements[i]) ;
uninitialized_fill(newelements+i , newelements+i+1 , elements[i]) ;
fill (newelements+i , newelements+i+1 , elements[i]) ;
If defined as 1 , unsafe iterator use causes a runtime error and the program is terminated. If defined as 0 , checked iterators are disabled. In debug mode, the default value for _SECURE_SCL is 1, meaning checked iterators are enabled. In release mode, the default value for _SECURE_SCL is 0.
By default , iterator debugging is enabled.
有问题代码为:uninitialized_copy(elements , first_free , newelements) ;
上述代码的目的是把elements到first_free之间的内存中的数据复制到以newelements开始的内存中
首先把这段代码放到vs 2010进行编译、运行你会发现只是弹出了如下的信息,但是能正确运行。
warning C4996: 'std::_Uninitialized_copy0': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
在vs 2012中进行编译会弹出如下信息:
error C4996: 'std::_Uninitialized_copy0': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
有上述代码的功能我想可以想到用如下的代码替代它:copy(elements , first_free , newelements) ;
此时你会发现在vs 2012中的编译信息如下:
error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
而在vs 2010中却仍可以正确编译并运行,所显示的编译信息如下:
warning C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
从编译的信息来看vs 2012有点矛盾的地方,也即error C4996 ... To disable this warning前面说是error后面却成了warning,逻辑上不太相符,当然这不排除是汉化引起的。
在vs 2012中不能像C++ PRIMER中所描述的那样使用这两个函数的原因是在vs 2012中提升了安全机制,在这里如果我们还是想直接使用这两个函数那么,我们可以在使用前添加如下的宏定义:
#ifndef _SECURE_SCL
#define _SECURE_SCL 0
#else
#undef _SECURE_SCL
#define _SECURE_SCL 0
#endif
或
#ifndef _ITERATOR_DEBUG_LEVEL
#define _ITERATOR_DEBUG_LEVEL 0
#else
#undef _ITERATOR_DEBUG_LEVEL
#define _ITERATOR_DEBUG_LEVEL 0
#endif
或者加入预处理器:(项目属性----C/C++----预处理----预处理器定义):
_SCL_SECURE_NO_WARNINGS
或者加入预处理器:(项目属性----C/C++----预处理----预处理器定义):
_SCL_SECURE_NO_WARNINGS
当然我们也可以不用这两个函数,而使用其它的函数来完成相同的功能,如以下几个函数:
1、用头文件algorithm中提供的模板函数copy(FwdIt first, FwdIt last, const T& x)来提供:
添加上述的宏定义,然后用
copy(elements , first_free , newelements) ;
2、用头文件memory中allocator类提供的操作constuct(p , t)来提供:
for(ptrdiff_t i=0 ; i
3、用头文件memory中提供的模板函数uninitialized_fill(FwdIt first, FwdIt last, const T& x)来提供:
for(ptrdiff_t i=0 ; i
4、用头文件algorithm中提供的模板函数fill(FwdIt first, FwdIt last, const T& x)来提供:
for(ptrdiff_t i=0 ; i
5、用头文件memory.h中提供的函数memcpy( void *dest, const void *src, size_t count )或memmove( void *dest, const void *src, size_t count )来提供:(T是所写的模板类的模板参数)
memcpy(newelements , elements , size*sizeof(T)) ;
或
memmove(newelements , elements , size*sizeof(T)) ;
下面对上面用到的宏进行简单的介绍:
_SECURE_SCL (SCL) macro defines whether Checked Iterators(Checked iterators ensure that the bounds of your container are not overwritten) are enabled.
_HAS_ITERATOR_DEBUGGING (HID) macro defines whether the iterator debugging feature is enabled in a debug build.
The following section describes the possible values of the SCL and HID macros :
SCL=0 Disables checked iterators.
SCL=1 Enables checked iterators.
HID=0 Disables iterator debugging in debug builds.
HID=1 Enables iterator debugging in debug builds. HID cannot be enabled in release builds.
The _ITERATOR_DEBUG_LEVEL (IDL) macro supersedes and combines the functionality of the _SECURE_SCL (SCL) and _HAS_ITERATOR_DEBUGGING (HID) macros.
The following table describes how the IDL macro values supersede the SCL and HID macro values :
Compilation mode
New macro
Old macros
Description
Debug
IDL=0
SCL=0, HID=0
Disables checked iterators and disables iterator debugging.
IDL=1
SCL=1, HID=0
Enables checked iterators and disables iterator debugging.
IDL=2 (Default)
SCL=(does not apply), HID=1
By default, enables iterator debugging; checked iterators are not relevant.
Release
IDL=0 (Default)
SCL=0
By default, disables checked iterators.
IDL=1
SCL=1
Enables checked iterators; iterator debugging is not relevant.
0 0
- [转] std::_Copy_impl
- std::_Copy_impl
- VS2013 error C4996: 'std::_Copy_impl'解决方法
- error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe
- 关于windows下编译caffe中blob C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe?
- c++ 中文std::string转std::wstring
- using namespace std(转)
- CString 转 std::string
- std::string 转LPCSTR
- std::bind 原理图(转)
- std::string 转 byte[]
- std string转wstring
- std
- std
- std
- std::stable_sort 和 std::for_each 的用法[转]
- [转]wchar_t char std::string std::wstring CString 转换
- [转]wchar_t char std::string std::wstring CString 转换
- VS 2005 查找功能无法使用的解决办法 VS2005重新配置
- /etc/sysconfig/network: No such file or directory
- undefined reference to `SSL_get_current_cipher\' 链接OpenSSL库 -lssl
- svn 不显示图标状态
- Deep Learning(深度学习)学习笔记整理系列之(四)
- [转] std::_Copy_impl
- error C2039: \'NotifyWinEvent\' : is not a member of \'`global namespace\'\'
- [转]Centos 网络配置(手动设置和自动获取)IP的方法
- vi vim 查找和替换字符串 命令
- [转]MySQL修改root密码的多种方法
- [转]The Adobe Flash plugin has crashed
- Unfortunately,Launcher has stopped
- [转] CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境
- <The Art of Readable Code> 笔记二 (上)
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
民族乐器展
北京乐器展
乐器行
乐器价格
乐器专卖店
乐器之王是什么乐器
乐器有什么
民族乐器专卖店
民族乐器商店
乐器店装修图片
敦煌乐器旗舰店
附近乐器店
乐器店
北京乐器店
附近的乐器店
西洋乐器店
勋乐器
乐器sun
乐器勋
损乐器
乐器损
大型乐器
便宜的乐器
飞翔乐器
其他乐器
乐器之后
民族乐器图片和名称
容易上手的乐器
民族乐器有哪些
交响乐团有哪些乐器
古典乐器有哪些
管乐器有哪些
民族乐器有什么
少数民族的乐器有哪些
中国的乐器有哪些
学什么乐器有前途
冷门乐器有哪些
中国古代乐器有哪些
女孩学什么乐器有发展
中国古典乐器有哪些
电子乐器有哪些