【问题标题】:Why does calling boost::split() give so many warnings?为什么调用 boost::split() 会给出这么多警告?
【发布时间】:2012-03-28 00:44:58
【问题描述】:

我需要一个在 dleimiter 上拆分字符串的函数,并且我将 boost 库用于其他事情,所以我尝试使用 boost::split。它有效,但它给了我一堆警告,我想知道为什么。

以下是在 MSVC++ 10 中产生警告的简化代码:

#include <tchar.h>
#include <iostream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
    std::vector<std::string> split_vector;
    boost::split(split_vector, "string,to,split", boost::is_any_of(","));
    for(size_t i=0;i<split_vector.size();i++)  {
        std::cout << split_vector[i] << "\n";
    }
}

大约有100行警告,我不知道如何在这里制作可折叠/可滚动的东西,但它们都是这样的:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2227): warning 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. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
c:\program files (x86)\microsoft visual studio 10.0\vc\include\xutility(2212) : see declaration of 'std::_Copy_impl'
c:\program files\boost\boost_1_49_0\boost\algorithm\string\detail\classification.hpp(102) : see reference to function template instantiation '_OutIt std::copy<const char*,char*>(_InIt,_InIt,_OutIt)' being compiled
with
[
    _OutIt=char *,
    _InIt=const char *
]
c:\program files\boost\boost_1_49_0\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
with
[
    CharT=char,
    IteratorT=const char *,
    RangeT=boost::iterator_range<const char *>
]
c:\users\administrator\documents\visual studio 2010\projects\cas testing\tests\tests.cpp(10) : see reference to function template instantiation 'boost::algorithm::detail::is_any_ofF<CharT> boost::algorithm::is_any_of<const char[2]>(RangeT (&))' being compiled
with
[
    CharT=char,
    RangeT=const char [2]
]

等等。

有人知道怎么回事吗?

【问题讨论】:

  • @Nawaz 它很可能也适用于 VS。他们只是有一些过度保护的警告。查看我链接到的可能的欺骗。 (ps.不知道ideone支持boost。不错)
  • 这只是 MS 将他们的“标准”强加于其他所有人。通过警告本身建议的路线禁用警告并将其称为良好。
  • 我不知道你为什么要在单个字符上使用is_any_of...这就是std::equal_to 的用途:)
  • @CodingAloneTogether:你必须绑定它:std::bind2nd(std::equal_to&lt;char&gt;(), ',')。 (是的,有点不那么优雅,但更高效,更接近你想要做的事情)

标签: c++ visual-c++ boost


【解决方案1】:

警告的第一行告诉你一切,包括为什么以及如何避免它,除此之外:要禁用此警告,请使用 -D_SCL_SECURE_NO_WARNINGS。因此,请转到项目属性,并将 _SCL_SECURE_NO_WARNINGS 放入预定义的宏中。

【讨论】:

  • 谢谢。我只是想确保在禁用警告之前我实际上没有做错任何事情。只需将 -D_SCL_SECURE_NO_WARNINGS 放在项目的命令行选项上即可禁用,但也可以将 _SCL_SECURE_NO_WARNINGS 声明为宏。
猜你喜欢
  • 2016-04-06
  • 2021-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 2014-09-25
  • 2013-05-28
相关资源
最近更新 更多