Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 32,082 of 33,346    |
|    Ulrich Eckhardt to Timothy Madden    |
|    Re: ios manipulator to print char/wchar_    |
|    02 Apr 12 00:36:03    |
      From: doomster@knuut.de              Timothy Madden wrote:       > Shoudn't there be an ios manipulator that prints the next charT       > (char/wchar_t/...) as the number ? Similar to the way boolalpha prints       > the next bool with a different format.              boolalpha prints all following bools with a textual representation. The       only non-sticky formatting flag is the width, IIRC.                     > I have run into this when using a template with an integral argument       > (the channel type for color values for image pixels), and much to my       > surprize if the type is int I see a number in the console, if it is       > unsigned char I see nothing...              Yes, wchar_t, unsigned char, signed char and "plain" char are all treated       differently. The way out is to static_cast them to unsigned int (or first       to unsigned char for the latter two).                     > Simply casting the value to (int) or other integral type is not enough       > since you do not know the signedness of the char type, and you do not       > even know the underlaying integral type for the given charT (an       > implementation is free to use long long to represent wchar_t ...)              Yes, this is indeed a problem. You can get around this though, using some       metaprogramming. For example, you can exploit that unsigned_type(-1) > 0       while signed_type(-1) < 0.                     > I ended up using       > cout << (channel_value + 0)       > but in a template I think this is still not generic enough, since it       > introduces requirements on the type for channel_value, which are not       > there with a simple       > cout << channel_value              Actually, I never thought of just adding a zero and relying on the type       promotion system doing the right thing, interesting approach! Otherwise,       what scalar type does not support "+0"? The only thing I could come up       with is std::string (which isn't even a scalar type).                     > Also I am not sure of the result type when wchar_t is int or unsigned,       > and I have (wchar_value + 0)              Just for the record: wchar_t, albeit its name, is _not_ a typedef, so it       can't be "int or unsigned [int]", only the integral type can be similar.              This is indeed a problem. You would probably sleep better if you wrote       tests that would fail on a platform where things are not as expected and       otherwise live with your not-fully-portable expectations for now. Further,       you could write template inserters for a tuple of channels and add       specializations for the few expected failure cases above.                     Uli                     --        [ See http://www.gotw.ca/resources/clcm.htm for info about ]        [ comp.lang.c++.moderated. First time posters: Do this! ]              --- SoupGate-Win32 v1.05        * Origin: you cannot sedate... all the things you hate (1:229/2)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca