Re: [Boost-users] format: show uint8_t as a number, not a char?
This does what I want:
uint8_t i = 65; printf("i = %d\n",i); [snip] uint8_t i = 65; cout << boost::format("i = %d\n") % i;
But it seems to have the same, unwanted, behaviour as cout<
cout << "i = " << static_cast<unsigned int>(i) << "\n";>
You can just cast the value to an int
Yes, as I mentioned towards the end of my message. But I find that obfuscated. It's particularly problematic when the value is a field in a struct whose type I don't exactly remember, and I forget the need to cast it; the result is control characters being written to the terminal, which can be hard to debug. Surely, since I have asked for %d in the format string, format() can do the cast? i.e. something like (PSEUDOCODE) template <typename T> operator%(T val) { .... if (format_char='d') int val1 = static_cast<int>(val) ... } or have I missed some additional complexity? Phil.
On 12/18/06, Phil Endecott
Surely, since I have asked for %d in the format string, format() can do the cast? i.e. something like (PSEUDOCODE)
template <typename T> operator%(T val) { .... if (format_char=='d') int val1 = static_cast<int>(val) ... }
or have I missed some additional complexity?
Well that will, for example, fail massively if T is a float or a rational or... Some hackery along the lines of if_< is_same< make_unsigned< remove_cv<T>::type >::type, unsigned char
, unsigned int, T > const &val1 = val; might work, but is ugly and would make format depend on mpl.
I'm not convinced that having it act like stringstream and lexical cast is a bad thing. Using a uint8_t in a situation where its numerical value would often be output in textual representation also smells of unnecessary size optimization, to me.
On 12/19/06, me22 wrote:
On 12/18/06, Phil Endecott
wrote: Surely, since I have asked for %d in the format string, format() can do the cast? i.e. something like (PSEUDOCODE)
I'm not convinced that having it act like stringstream and lexical cast is a bad thing. Using a uint8_t in a situation where its numerical value would often be output in textual representation also smells of unnecessary size optimization, to me.
OTOH, "This class's goal is to bring a better, C++, type-safe and type-extendable printf equivalent to be used with streams." (http://boost.org/libs/format/doc/format.html#rationale) Being equivalent means that it should "provide printf-compatibility, as much as it makes sense in a type-safe and type-extendable context." (http://boost.org/libs/format/doc/format.html#rationale) Given the above stated goals of boost::format, the following should be equivalent unless that wouldn't make sense. [quoted from OP] uint8_t i = 65; printf("i = %d\n",i); uint8_t i = 65; cout << boost::format("i = %d\n") % i; I don't see any reason why it wouldn't make sense for these to be equivalent, so I think they should be. If someone does have a reason why it wouldn't make sense, then that reason should be added to the rationale section of the documentation, as the current behavior goes counter to a very reasonable expectation. -- Todd Greer
participants (3)
-
me22
-
Phil Endecott
-
Todd Greer