C++ Boost:这个警告的原因是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1301277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Boost: what's the cause of this warning?
提问by sivabudh
I have a simple C++ with Boost like this:
我有一个带有 Boost 的简单 C++,如下所示:
#include <boost/algorithm/string.hpp>
int main()
{
std::string latlonStr = "hello,ergr()()rg(rg)";
boost::find_format_all(latlonStr,boost::token_finder(boost::is_any_of("(,)")),boost::const_formatter(" "));
This works fine; it replaces every occurrence of ( ) , with a " "
这很好用;它将 ( ) 的每次出现替换为 " "
However, I get this warning when compiling:
但是,我在编译时收到此警告:
I'm using MSVC 2008, Boost 1.37.0.
我使用的是 MSVC 2008,Boost 1.37.0。
1>Compiling...
1>mainTest.cpp
1>c:\work\minescout-feat-000\extlib\boost\algorithm\string\detail\classification.hpp(102) : warning C4996: 'std::copy': Function call 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'
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(2576) : see declaration of 'std::copy'
1> c:\work\minescout-feat-000\extlib\boost\algorithm\string\classification.hpp(206) : see reference to function template instantiation 'boost::algorithm::detail::is_any_ofF<CharT>::is_any_ofF<boost::iterator_range<IteratorT>>(const RangeT &)' being compiled
1> with
1> [
1> CharT=char,
1> IteratorT=const char *,
1> RangeT=boost::iterator_range<const char *>
1> ]
1> c:\work\minescout-feat-000\minescouttest\maintest.cpp(257) : see reference to function template instantiation 'boost::algorithm::detail::is_any_ofF<CharT> boost::algorithm::is_any_of<const char[4]>(RangeT (&))' being compiled
1> with
1> [
1> CharT=char,
1> RangeT=const char [4]
1> ]
I could certainly disable the warning using
我当然可以使用禁用警告
-D_SCL_SECURE_NO_WARNINGS
but I'm a bit reluctant to do that before I find out what's wrong, or more importantly if my code is incorrect.
但在我发现问题之前,或者更重要的是,如果我的代码不正确,我有点不愿意这样做。
回答by jalf
It is nothing to worry about. In the last few releases of MSVC, they've gone into full security-paranoia mode. std::copy
issues this warning when it is used with raw pointers, because when used incorrectly, it can result in buffer overflows.
没什么好担心的。在 MSVC 的最后几个版本中,他们进入了完全的安全偏执模式。std::copy
与原始指针一起使用时会发出此警告,因为如果使用不当,可能会导致缓冲区溢出。
Their iterator implementation performs bounds checking to ensure this doesn't happen, at a significant performance cost.
他们的迭代器实现执行边界检查以确保不会发生这种情况,但性能成本很高。
So feel free to ignore the warning. It doesn't mean there's anything wrong with your code. It is just saying that ifthere is something wrong with your code, then bad things will happen. Which is an odd thing to issue warnings about. ;)
所以请随意忽略警告。这并不意味着您的代码有任何问题。这只是说如果你的代码有问题,那么糟糕的事情就会发生。发出警告是一件奇怪的事情。;)
回答by Bizmarck
You can also disable this warning in specific headers:
您还可以在特定标题中禁用此警告:
#if defined(_MSC_VER) && _MSC_VER >= 1400
#pragma warning(push)
#pragma warning(disable:4996)
#endif
/* your code */
#if defined(_MSC_VER) && _MSC_VER >= 1400
#pragma warning(pop)
#endif
回答by Bluebaron
If you feel safe about disabling this error:
如果您对禁用此错误感到安全:
- Go to the properties of your C++ project
- Expand the "C/C++"
- Highlight "Command Line"
- Under "Additional Options" append the following to any text that might be in that box
- 转到 C++ 项目的属性
- 展开“C/C++”
- 突出显示“命令行”
- 在“其他选项”下,将以下内容附加到该框中可能存在的任何文本中
" -D_SCL_SECURE_NO_WARNINGS"
“ -D_SCL_SECURE_NO_WARNINGS”
回答by fbrereto
The warning comes from Visual Studio's non-standard "safe" library checks introduced starting with MSVC 8.0. Microsoft has identified "potentially dangerous" APIs and has injected warnings discouraging their use. While it is technically possible to call std::copy in an unsafe way, 1) receiving this warning does not mean you are doing so, and 2) using std::copy as you normally should is not dangerous.
该警告来自从 MSVC 8.0 开始引入的 Visual Studio 的非标准“安全”库检查。Microsoft 已经确定了“潜在危险”的 API 并注入了警告,以阻止它们的使用。虽然在技术上可以以不安全的方式调用 std::copy,但 1) 收到此警告并不意味着您正在这样做,并且 2) 正常使用 std::copy 并不危险。
回答by sailfish009
Go to the properties of your C++ project
Expand the "C/C++"
Advanced: Disable Specific Warnings: 4996
转到 C++ 项目的属性
展开“C/C++”
高级:禁用特定警告:4996
回答by Alex Azzalini
Alternatively, if you use C++11 and don't want to turn off warnings, you have the painful option of replacing
或者,如果您使用 C++11 并且不想关闭警告,您可以选择替换
boost::is_any_of(L"(,)")
with the following lambda expression
使用以下 lambda 表达式
[](wchar_t &c) { for (auto candidate : { L'(', L',', L')' }) { if (c == candidate) return true; }; return false; }
You can also possibly pack that into a macro
你也可以把它打包成一个宏