Boost Format: format strings and character types
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.
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
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
In article <7253f6b30801300934j29aa2c09ib5c07256a50a38ff@mail.gmail.com>, Chun ping wang wrote:
I think the first one won't work because %d is for decimal printing , and char is a character.
Well ... yes, that's right. char is a character, and what I want to do is to print its decimal value. We're using a char here as an 8-bit int not to hold a character.
Format is a typesafe library so the document should say any type safe format will work in here as in printf.
char to int is a safe conversion ... the documentation leads me to expect that it will work in format the same way as it does in printf, but it doesn't. The question is: is the code wrong, or is the documentation misleading?
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;
No: %2d$ gives exactly the same as %2% or %d. The only workaround I've found is to cast the char variable to int. Cheers, Daniel
participants (2)
-
chun ping wang
-
Daniel James