vs2013 error:C4996

来源:互联网 发布:手机网络制式有几种 编辑:程序博客网 时间:2024/05/17 23:53
     在学习如何编写移动构造函数的时候,照着文中的代码敲,用vs013编译,出现 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'

解决办法如下:

方法一:
在出现该错误的文件中添加:#pragma warning(disable : 4996)     不理它
注意:1)添加位置:在包含头文件语句前添加该语句;
          2)#pragma warning(disable:4996)只对当前文件(包括包含了当前文件的文件)起作用,并非对整个工程 。

方法二:
在包含头文件语句前添加:#define _SCL_SECURE_NO_WARNINGS

方法三:
加入预处理器(项目属性--C/C++--预处理--预处理定义)
_SCL_SECURE_NO_WARNINGS

方法四:
修改代码:
// 拷贝构造函数MemoryBlock(const MemoryBlock& other):_length(other._length), _data(new int[other._length]){std::copy(other._data, other._data + _length, stdext::checked_array_iterator<int*>(_data,_length)/*_data*/);std::cout << "Copy Constructor" << std::endl;}


原因:
     不安全的函数,象vector的下标操作算子,如果不恰当的调用,通常会导致不明确的行为。为了保护你的发行版本的应用程序,Visual C++2005便引入了_SECURE_SCL符号,用来给那些非安全的函数添加运行时检查。
     如果_SECURE_SCL定义为1,不安全的参数使用会造成error,程序终止。如果定义为0,禁用“经检查的迭代器”(checked iterators:Checked iterators ensure that the bounds of your container are not overwritten.)。默认情况下,_SECURE_SCL在release版是0,在debug版是1。
     以上述代码中的std::copy(first, last, destination)为例;其中,first和last是定义拷贝范围的迭代参数,destination是输出迭代参数,指示了目标缓冲区的位置。这里有一个潜在危险是destination所对应的目标缓冲区或容器不足够大,无法容纳所要拷贝的元素。如果Destination是一个需要安全检查的迭代参数,类似的错误将被捕获。但是,这仅仅是一个假设。如果destination是一个简单的指针,将无法保证copy运算函数正确运转。
     相对而言,方法四更安全,前三种方法只是忽略了这种潜在危险。

关于checked iterators的扩展阅读,可参考:
[1]Checked Iterators[msdn]
[2] C++/CLI中的Checked Iterators

-------------------------------------------------
参考资料:
[1] error C4996: Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct
[2] _SCL_SECURE_NO_WARNINGS[msdn]
[3] 解决use -D_SCL_SECURE_NO_WARNINGS的问题
[4] vs2013编译 protoBuffer编译出现的问题


0 0
原创粉丝点击