• C++primer 5 中 uninitialized_copy函数报错的问题


    c++primer 中的一个函数报错的问题

    StrVec类的设计中定义这个类,定义了一个static变量alloc,用来分配内存和构造元素

    class StrVec
    {
    public:
     StrVec() :elements(nullptr), first_free(nullptr), cap(nullptr) {}
     StrVec(initializer_list<string> li);
     StrVec(const StrVec&);
     StrVec& operator= (const StrVec&);
     ~StrVec();
     void push_back(const string&);         // 拷贝元素
     size_t size() const { return first_free - elements; }
     size_t capacity() const { return cap - elements; }
     string *begin() const{ return elements; }
     string *end() const { return first_free; }
     void reserve(size_t n);
     void resize(size_t n);
     void resize(size_t n, string str);
    private:
      static allocator<string> alloc;      // 静态成员,分配

    pair<string*, string*> alloc_n_copy(const string*, const string*);             // 分配内存,拷贝元素

    在实现函数alloc_n_copy的时候

    pair<string *, string*> StrVec::alloc_n_copy(const string *b, const string *e)
    {
     auto data =alloc.allocate(e - b);
     return{ data,uninitialized_copy(b,e,data) };        
    }

    在调用uinitialized_copy函数时,会产生不安全的警告:

    错误 C4996 'std::uninitialized_copy::_Unchecked_iterators::_Deprecate': Call to 'std::uninitialized_copy' 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' 

    对参数的调用可能不安全,需要调用这个函数的函数来保证迭代器的值是正确的。程序会报错。

    去掉static声明,加上宏命令#define  _SCL_SECURE_NO_WARNINGS(.cpp文件中)可以去掉这个警告

  • 相关阅读:
    安卓学习12
    安卓学习11
    安卓学习10
    安卓学习9
    Python3之json&pickle模块
    Mysql之基础sql语句
    Django模型层之单表操作
    创建Django项目与应用的两个命令
    windows命令行切换目录
    Django视图层之请求对象(request)和响应对象(HttpResponse)
  • 原文地址:https://www.cnblogs.com/sanerer/p/7834735.html
Copyright © 2020-2023  润新知