C++primer 5 中 uninitialized_copy函數報錯的問題

來源:互聯網
上載者:User

標籤:back   param   bsp   靜態成員   c++primer   call   size_t   city   unsafe   

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檔案中)可以去掉這個警告

 

C++primer 5 中 uninitialized_copy函數報錯的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.