[英]error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe
I know this question was asked numerous times in SO, but this is a variation from the rest. 我知道这个问题在SO中被多次询问过,但这与其他问题有所不同。
Compiler Error: Function call with parameters that may be unsafe 编译器错误:带有可能不安全的参数的函数调用
Visual Studio Warning C4996 Visual Studio警告C4996
xutility(2227): warning C4996: 'std::_Copy_impl' xutility(2227):警告C4996:'std :: _ Copy_impl'
Failing Code Snippet 失败的代码片段
DWORD dwNumberOfNames = pExportDirectory->NumberOfNames;
LPDWORD dwNames = (LPDWORD)((LPBYTE)hDLL + pExportDirectory->AddressOfNames);
std::vector< std::string > exports;
std::copy(
dwNames,
dwNames + dwNumberOfNames,
[&exports, &hDLL](DWORD dwFuncOffset)
{
std::string fname = std::string((PCHAR)((PBYTE)hDLL + dwFuncOffset));
exports.push_back(fname);
}
);
Compiler Error 编译错误
Error 1 error C4996: '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.
错误1错误C4996:'std :: _ Copy_impl':带有可能不安全的参数的函数调用 - 此调用依赖于调用者来检查传递的值是否正确。 To disable this warning, use -D_SCL_SECURE_NO_WARNINGS.
要禁用此警告,请使用-D_SCL_SECURE_NO_WARNINGS。 See documentation on how to use Visual C++ 'Checked Iterators' C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\include\\xutility 2176
请参阅有关如何使用Visual C ++'Checked Iterators'的文档:C:\\ Program Files(x86)\\ Microsoft Visual Studio 11.0 \\ VC \\ include \\ xutility 2176
Question 题
Considering, C4996 , means the function was marked to be deprecated Where is the problem? 考虑到, C4996 ,意味着该功能已标记为已弃用问题在哪里?
std::copy
with a C Array
? C Array
std::copy
吗? Note 注意
I know, how to suppress the warning, but I am curious to know, the root cause of the problem? 我知道,如何压制警告,但我很想知道,问题的根本原因是什么?
Also, equally important for me to know, the portable way to handle this issue without compromising code quality. 此外,同样重要的是,我知道,在不影响代码质量的情况下处理此问题的便携方式。
You aren't calling std::copy
according to msdn: http://msdn.microsoft.com/en-us/library/x9f6s1wf.aspx 你不是根据msdn调用
std::copy
: http : //msdn.microsoft.com/en-us/library/x9f6s1wf.aspx
That function signature is this: 那个函数签名是这样的:
template<class InputIterator, class OutputIterator>
OutputIterator copy(
InputIterator _First,
InputIterator _Last,
OutputIterator _DestBeg
);
There's no place for a functor/lambda there. 那里没有functor / lambda的地方。
You're not calling std::copy_if
either: http://msdn.microsoft.com/en-us/library/ee384415.aspx 你也不是在调用
std::copy_if
: http : //msdn.microsoft.com/en-us/library/ee384415.aspx
template<class InputIterator, class OutputIterator, class BinaryPredicate>
OutputIterator copy_if(
InputIterator _First,
InputIterator _Last,
OutputIterator _Dest,
Predicate _Pred
);
As you have no output iterator, nor does your predicate return a bool. 因为你没有输出迭代器,你的谓词也没有返回bool。
It looks like you want std::transform
: http://msdn.microsoft.com/en-us/library/391xya49.aspx 看起来你想要
std::transform
: http : //msdn.microsoft.com/en-us/library/391xya49.aspx
template<class InputIterator, class OutputIterator, class UnaryFunction>
OutputIterator transform(
InputIterator _First1,
InputIterator _Last1,
OutputIterator _Result,
UnaryFunction _Func
);
And you should be returning the value you want in the output iterator. 你应该在输出迭代器中返回你想要的值。 So it'll be something like this:
所以它会是这样的:
std::transform(
dwNames,
dwNames + dwNumberOfNames,
std::back_inserter(exports),
[&hDLL](DWORD dwFuncOffset) // This lambda is WRONG
{
// THIS LINE IS WRONG
std::string fname = std::string((PCHAR)((PBYTE)hDLL + dwFuncOffset));
return fname;
}
);
My lambda is wrong. 我的lambda错了。 You need the input of the ELEMENTS of your array (I'm not sure of the type) as the argument to the lambda, and return what you want inserted into the
exports
vector. 您需要输入数组的ELEMENTS(我不确定类型)作为lambda的参数,并返回要插入到
exports
向量中的内容。
You probably also didn't know about back_inserter
. 你可能也不知道
back_inserter
。 It's in the <iterator>
header. 它位于
<iterator>
标头中。 See here: http://msdn.microsoft.com/en-us/library/12awccbs.aspx and here: http://www.cplusplus.com/reference/iterator/back_inserter/ 请参阅此处: http : //msdn.microsoft.com/en-us/library/12awccbs.aspx ,此处: http : //www.cplusplus.com/reference/iterator/back_inserter/
That may not be 100% of the answer, but with that, I think you can get to where you want to go. 这可能不是100%的答案,但有了这个,我想你可以到达你想去的地方。
It is not std::copy
itself, I think your problem is to use LPDWORD to do the copy which makes Visuall C++ thinks you are doing C string copy because LPDWORD
is not a checked iterator. 它不是
std::copy
本身,我认为你的问题是使用LPDWORD进行复制,这使得Visuall C ++认为你正在进行C字符串复制,因为LPDWORD
不是一个经过检查的迭代器。
http://msdn.microsoft.com/en-us/library/ttcz0bys(v=vs.120).aspx http://msdn.microsoft.com/en-us/library/ttcz0bys(v=vs.120).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.