1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VC++で'std::_Copy_impl': Function call with parameters that may be unsafe と言われたときの対処

Posted at

VC++でこんな感じのコードをコンパイルしたとき

void Func(int* dest, int size)
{
  std::vector<int> src(size);
  std::copy(src.cbegin(), src.cend(), dst);
}

以下のような警告文が出てきたときの対処方法。

'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.

std::copyにイテレータではなくポインタを渡していたことが原因でした。
配列であり、そのサイズもわかっているなら以下のようにすることでイテレータに変換できるようです。

std::copy(src.cbegin(), src.cend(), stdext::checked_array_iterator<int*>(dst, size));

参考サイト

Microsoft Developer Network - checked_array_iterator クラス

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?