A problem with an error in a function in C++primer
This class is defined in the design of the Strvec class and defines a static variable alloc, which is used to allocate memory and construct elements
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&); Copy elements
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; static members, assigning
pair<string*, string*> alloc_n_copy (const string*, const string*); allocating memory, copying elements
At the time of implementing the function 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)};
}
An unsafe warning is generated when the Uinitialized_copy function is called:
Error C4996 ' std::uninitialized_copy::_unchecked_iterators::_deprecate ': Call to ' std::uninitialized_copy ' with Parameters that is Unsafe-this call relies on the caller to check that the passed values is correct. To disable this warning, use-d_scl_secure_no_warnings. See documentation "On" use Visual C + + ' Checked iterators '
Calls to parameters may be unsafe, and the function's functions need to be called to ensure that the value of the iterator is correct. The program will error.
Remove the static declaration, plus the macro command # define _SCL_SECURE_NO_WARNINGS (. cpp file) to remove this warning
The problem of uninitialized_copy function error in C++primer 5