Ryan McCleary
Ryan McCleary

Reputation: 371

How do I deal with warnings generated by Boost.Spirit?

I have recently installed boost, and i was experimenting with the Spirit library. I compiled a simple example which parses a comma-seperated list of numbers and adds them together. The program compiled, but my compiler (VS 2013) issued a rediculous amount of warnings. Have a look at the source:

#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;
}

This generated 309 lines of warnings! They all looked similar to this:

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

The program compiled fine and did what I thought it would do, but I'm wondering how to manage all of these warnings without silencing the useful ones. Is there a way to disable warnings originating from boost, but to preserve the warnings generated by my code? Spirit is a fairly popular library, so I know that there's some way to deal with it.

Upvotes: 4

Views: 806

Answers (1)

ildjarn
ildjarn

Reputation: 62975

With VC++ you need to wrap your Boost includes in a few pragmas:

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

// ...

Add to the disable list as needed, space delimited (docs).

Other compilers typically allow you to tag specified include paths as 'system' paths, and all warnings from headers in system paths are suppressed. For GCC and Clang in particular, use -isystem rather than -I (docs).

Upvotes: 6

Related Questions