gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:13:01 24 4
gpt4 key购买 nike

我有一个像这样的带有 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/13275129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com