【问题标题】:Error c4996 Checked Iterators错误 c4996 检查迭代器
【发布时间】:2014-02-09 04:58:56
【问题描述】:

我使用 VC++ 2013,我有以下代码:

#pragma warning(disable:4996)
#define D_SCL_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <object.pb.h>

using namespace std;

int main(int argc, char** argv)
{
    Object object;
    object.set_id(1);
    object.set_name("Ermolaev Ivan");
    object.set_vertex(300.0f);
    fstream output("myfile", ios::out | ios::binary);
    object.SerializeToOstream(&output);
    return 0x0;
}

但继续显示以下错误。

Error   1   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'   c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 2132    1   ProtobufTest

【问题讨论】:

    标签: c++ visual-c++


    【解决方案1】:

    如果您使用 selfmaden 迭代器,则 typedef iterator _Unchecked_type; 在自定义迭代器内,让 microsoft std 库相信它是 checket。

    class iterator: public std::iterator<std::input_iterator_tag, const uint8_t>
    {
    public:
    
        typedef iterator _Unchecked_type;
    // your implementation here
    }
    

    【讨论】:

    • 谢谢,我花了很多时间才找到这个信息。唯一的缺点是显然将我的代码耦合到 MSVC/Dinkumware 的 STL 实现。
    • 几个月来我一直在寻找这个信息!谢谢。
    【解决方案2】:

    D前缀不是必须的,它是你在命令行引入define时编译器的一个指示符。

    您应该简单地将其定义为_SCL_SECURE_NO_WARNINGS。最好在 Project Properties -&gt; C++ -&gt; Preprocessor 中执行此操作,以使其在所有代码中保持一致。

    【讨论】:

    • 澄清一下,-D是要定义的前缀,_SCL_SECURE_NO_WARNINGS是要定义的宏
    猜你喜欢
    • 2016-04-09
    • 2023-03-13
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 2011-07-22
    • 1970-01-01
    • 2020-03-19
    相关资源
    最近更新 更多