Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#6852 closed Bugs (fixed)

lexical_cast: warning: ISO C++ does not support the ‘%lg’ gnu_printf format

Reported by: peter@… Owned by: Antony Polukhin
Milestone: To Be Determined Component: lexical_cast
Version: Boost 1.49.0 Severity: Cosmetic
Keywords: Cc:

Description

The following code produces a warning if compiled with GCC (4.7.0) and -Wall -std=c++98 -pedantic:

include <boost/lexical_cast.hpp>

int main()
{
    boost::lexical_cast<std::string>(double(4.2));
    return 0;
}
boost_1_49_0/include/boost/lexical_cast.hpp:1297:17: warning: ISO C++ does not support the ‘%lg’ gnu_printf format [-Wformat]

With regard to the length modifier, the printf(3) man page (RHEL 6.2) states:

       l      (ell) A following integer conversion corresponds to a  long  int
              or  unsigned long int argument, or a following n conversion cor-
              responds to a pointer to a long int argument, or a  following  c
              conversion  corresponds  to  a wint_t argument, or a following s
              conversion corresponds to a pointer to wchar_t argument.

With regard to the conversion specifier:

       g, G   The  double argument is converted in style f or e (or F or E for
              G conversions).  The precision specifies the number of  signifi-
              cant  digits.   If the precision is missing, 6 digits are given;
              if the precision is zero, it is treated as 1.  Style e  is  used
              if  the  exponent from its conversion is less than -4 or greater
              than or equal to the precision.  Trailing zeros are removed from
              the  fractional part of the result; a decimal point appears only
              if it is followed by at least one digit.

So the correct printf format for double is "%g".

(Maybe this is different on non-GNU systems?)

Change History (2)

comment:1 by Antony Polukhin, 10 years ago

Resolution: fixed
Status: newclosed

(In [78308]) Fixes #6852 (ISO C++ does not support the ‘%lg’ gnu_printf format fixed)

comment:2 by Antony Polukhin, 10 years ago

(In [78408]) Fixes #6852 for wide characters (ISO C++ does not support the ‘%lg’ gnu_printf format fixed)

Note: See TracTickets for help on using tickets.