【问题标题】:How do I deal with warnings generated by Boost.Spirit?如何处理 Boost.Spirit 产生的警告?
【发布时间】:2016-04-15 00:55:56
【问题描述】:

我最近安装了 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 是一个相当受欢迎的库,所以我知道有一些方法可以处理它。

【问题讨论】:

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


    【解决方案1】:

    使用 VC++,您需要将 Boost 包含在几个 pragma 中:

    #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)。

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

    【讨论】:

    • 他们为什么不将这些编译指示放入 boost 本身? (#ifdef 如果有必要,但 pragma 的全部意义在于您不必这样做)我在使用 VC++ 2015 通过 b2 编译 boost 1.60.0 时看到了一百万个,这使安装过程变得一团糟。
    • @WarrenP :这是非常不切实际的,因为编译器诊断(对于任何编译器,而不仅仅是 VC++)会改变每个编译器版本,并且 Boost 支持十几个编译器。所涉及的工作量是巨大的。在 2010 年,Boost 确实有一个社区范围的“警告清理驱动器”,持续了几个月并修复了数千个警告,但随着开发的进行而保持其最重要的是一个几乎无法克服的问题。
    • 我用了大约 5 分钟在本地清理了它们。也许我会向 Boost 中出色的 Haxxors 发送拉取请求。我不知道有任何编译器会在不理解的 pragma warn 指令上产生错误。
    • @WarrenP : 消除警告并不是“清理”任何东西,它只是关闭一些东西;如果要进行真正的修复,请首先修复导致警告的原因。这样,您将帮助所有编译器,而不仅仅是您现在正在使用的编译器版本。 #pragma warning 是代码污染,用于解决您无法修复的代码; Boost 是开源的,所以只需实际修复它。
    • 为此,我想我需要understand_problem&lt;What&lt;The,Heck&gt;::Is_Going&lt;On,InThere&gt;&gt;,但我不需要。这是一个奇怪的警告,我想我会试着弄清楚它到底意味着什么。
    猜你喜欢
    • 2017-10-18
    • 1970-01-01
    • 2015-02-06
    • 2020-06-02
    • 2013-03-30
    • 2021-11-25
    • 2018-12-25
    • 2015-07-22
    • 2016-09-19
    相关资源
    最近更新 更多