C++ Boost:这个警告的原因是什么?

2021-12-24 00:00:00 c++ boost compiler-warnings

我有一个带有 Boost 的简单 C++,如下所示:

I have a simple C++ with Boost like this:

#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:

我使用的是 MSVC 2008,Boost 1.37.0.

I'm using MSVC 2008, Boost 1.37.0.

1>Compiling...
1>mainTest.cpp
1>c:workminescout-feat-000extliboostalgorithmstringdetailclassification.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.0vcincludexutility(2576) : see declaration of 'std::copy'
1>        c:workminescout-feat-000extliboostalgorithmstringclassification.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:workminescout-feat-000minescouttestmaintest.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>        ]

我当然可以使用

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

推荐答案

没什么好担心的.在 MSVC 的最后几个版本中,他们进入了完全的安全偏执模式.std::copy 与原始指针一起使用时会发出此警告,因为如果使用不当,会导致缓冲区溢出.

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.

他们的迭代器实现执行边界检查以确保不会发生这种情况,但性能成本很高.

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 if there is something wrong with your code, then bad things will happen. Which is an odd thing to issue warnings about. ;)

相关文章