Search code examples
c++boostwarningsisolexical-cast

boost::lexical_cast compiles with warnings


Compiling the following:

// file main.cpp

#include <string>
#include <boost/lexical_cast.hpp>

int main()
{
    boost::lexical_cast<std::string>( 656.16 );
    return 0;
}

yields to this:

/usr/local/include/boost/lexical_cast.hpp:1184: warning: ISO C++ does not support the ‘%lg’ printf format

Why? How to avoid this warning?


Solution

  • I GCC, there’s a simple trick to silence all warnings from a specific library’s headers. Simply treat them as system headers. This is safe, as far as I know, and I use it as a course of standard practice.

    That is, tweak your command line to use -isystem instead of -I to specify the Boost header location. Everything else stays the same.

    For instance, this is taken from a Makefile of one of my projects:

    BOOST=/usr/local/Cellar/boost/1.48.0
    CXXFLAGS+=-isystem$(BOOST)
    

    Note: it is important that you don’t silence warnings in general in your projects. On the contrary, you should treat all warnings as errors (-Werror) and warn as much as possible (.e.g. -Wall -Wextra). The above disables only those warnings caused by Boost, not from your own code. This is as it should be.