C++ boost : what's the cause of this warning?

标签 c++ boost compiler-warnings

我有一个带有 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(" "));

这很好用;它将 ( ) 的每次出现替换为 ""

但是,我在编译时收到此警告:

我使用的是 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>        ]

我当然可以使用

禁用警告
-D_SCL_SECURE_NO_WARNINGS

但在我发现问题所在或更重要的是,如果我的代码不正确之前,我有点不愿意这样做。

最佳答案

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

他们的迭代器实现会执行边界检查以确保不会发生这种情况,但会付出巨大的性能代价。

所以请随意忽略警告。这并不意味着您的代码有任何问题。这只是说如果你的代码有问题,那么坏事就会发生。 发出警告是一件奇怪的事情。 ;)

关于C++ boost : what's the cause of this warning?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1301277/

相关文章:

c++ - 获取对 unordered_map operator[] 返回值的引用

c++ - 有没有办法根据模板参数的类型在不同的类实现之间进行选择?

c++ - std::pair 提示类型不完整

c++ - iOS:更新到 Xcode 8.3 后编译器中的 C++ 警告

c# - 错误的 C# 编译器警告?

xcode - 使 Xcode (swift) 为 print() 抛出错误

c++ - 如何将带有冒号的输入文件读入数组?

c++ - 预构造函数初始化

c++ - 引用c++临时字符串的内容

c++ - 数据模型问题