c++ - Need a clean replacement to a possibly unsafe call to std::copy -


i know there better options, std::vector or std::array etc. in case, have use pointer dynamically allocated array because i'm learning copy-and-swap idiom , involves creating own resource management class.

say have following copy constructor resource handle class:

a(const a& other) : size(other.size), arr(size ? new int[size]() : nullptr) {     std::copy(other.arr, other.arr + size, arr); } 

it doesn't compile in visual studio (2013 preview nor 2012 express). error i'm getting is: enter image description here

is possible use std::copy in way compiler stops yelling @ me? or better idea manually copy contents of array using simple loop like

for (int = 0; < size; i++)      arr[i] = other.arr[i]; 

p.s. don't want use hacks/macros disable warnings etc.

you have several options:

1) define d_scl_secure_no_warnings error message says. also, have "make warning errors" turned on? seems should warning, not error. (edit: see don't want use approach).

2) use checked_array_iterator (however, not standard):

std::copy(other.arr, other.arr + size, stdext::checked_array_iterator<int*>(arr, size)); 

3) use std::vector best approach.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -