gpt4 book ai didi

c++ - 如何处理 Boost.Spirit 生成的警告?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:35:20 24 4
gpt4 key购买 nike

我最近安装了 boost,并且正在试验 Spirit 库。我编译了一个简单的例子,它解析一个逗号分隔的数字列表并将它们加在一起。该程序已编译,但我的编译器 (VS 2013) 发出了大量警告。查看源代码:

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <string>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <string>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;

using qi::double_;
using qi::_1;
using ascii::space;
using phoenix::ref;

template <typename Iterator>
bool adder(Iterator first, Iterator last, double& n)
{
bool r = qi::phrase_parse(first, last,

// Begin grammar
(
double_[ref(n) = _1] >> *(',' >> double_[ref(n) += _1])
)
,
// End grammar

space);

if (first != last) // fail if we did not get a full match
return false;
return r;
}

int main()
{
std::string str;
std::getline(std::cin, str);
double result;
if (!adder(str.begin(), str.end(), result))
{
std::cout << "Invalid syntax." << std::endl;
}
std::cout << "The result is " << result << std::endl;
return 0;
}

这产生了 309 行警告!它们看起来都像这样:

c:\boost\boost/spirit/home/support/terminal.hpp(264) : warning C4348: 'boost::spirit::terminal<boost::spirit::tag::lit>::result_helper' : redefinition of default parameter : parameter 3
c:\boost\boost/spirit/home/support/terminal.hpp(270) : see declaration of 'boost::spirit::terminal<boost::spirit::tag::lit>::result_helper'
c:\boost\boost/spirit/home/support/common_terminals.hpp(142) : see reference to class template instantiation 'boost::spirit::terminal<boost::spirit::tag::lit>' being compiled

该程序编译得很好并且做了我认为它会做的事情,但我想知道如何管理所有这些警告而不会使有用的警告消失。有没有办法禁用源自 boost 的警告,但保留我的代码生成的警告? Spirit 是一个相当受欢迎的库,所以我知道有一些方法可以处理它。

最佳答案

使用 VC++,您需要将 Boost 包含在几个编译指示中:

#pragma warning(push)
#pragma warning(disable : 4348)
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#pragma warning(pop)

#include <iostream>
#include <string>

// ...

根据需要添加到 disable 列表,以空格分隔 (docs)。

其他编译器通常允许您将指定的包含路径标记为“系统”路径,并禁止来自系统路径中 header 的所有警告。特别是对于 GCC 和 Clang,请使用 -isystem 而不是 -I ( docs )。

关于c++ - 如何处理 Boost.Spirit 生成的警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34709284/

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