I think the first one won't work because %d is for decimal printing , and char is a character. Format is a typesafe library so the document should say any type safe format will work in here as in printf. But interesting find.
Instead this would.
printf( "Using printf: ii=%d, cc=%d\n", ii, cc );
cout << boost::format( "Using boost::format: ii=%1d%, cc=%2d%" ) %
ii % cc << endl;
On Jan 30, 2008 4:29 AM, Daniel James <
gmane.junk001@sonadata.co.uk> wrote:
The documentation states that the same format string used with printf
and boost::format should result in the same output in "almost all
cases".
My colleagues and I have found, though, that when a %d descriptor is
used to print a char variable the result is as though a %c descriptor
had been used -- printf and boost::format don't produce the same
result.
That is:
int ii(42);
char cc(42);
printf( "Using printf: ii=%d, cc=%d\n", ii, cc );
cout << boost::format( "Using boost::format: ii=%d, cc=%d" ) %
ii % cc << endl;
Produces:
Using printf: ii=42, cc=42
Using boost::format: ii=42, cc=*
So, it seems that boost::format deduces the format to be used for
outputting cc from its type and ignores the format explicitly requested
in the format.
Is this working as intended? If so the documentation seems to me to be
misleading.
I also note that:
cout << boost::format( "As chars: ii=%c, cc=%c" ) % ii % cc << endl;
Produces:
As chars: ii=4, cc=*
That is: the %c descriptor causes the value of ii (42) to be truncated
to a single char. Also something of a surprise.
My tests were carried out using boost 1.34.1 and Visual Studio 2005 on
Windows.
Cheers,
Daniel.
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users