c++ - Create array of chars avoiding narrowing -
I am writing a unit test, which checks some binary data against an expected array. The expected array in question is a series of bytes, especially if it does not matter:
char expected [=] {0x42, 0xde, 0xad, 0xbe, 0xef}; It's okay to compile in C ++, but with C ++ 11 it issues a warning on compressed conversions. I compile with -wire because the warnings are important, so this line does not compile for me as far as I know, there is no real suffix for all, so it seems That i will have to:
char expected [] = {static_cast & lt; Char & gt; (0x42), static_cast & lt; Four & gt; (0xde), ...}; It seems very unwieldy for me Is this a better way to create character array? (Either by removing -varity or -vino-narrow ].
In the target type for the integer type and unscoped enums for C + 11, draft C ++ 11 standard section 8.5.4 [dcl.init.list] says:
An integer type with an integer type or uncompressed calculation type can not All values of the original type represent, except that the source is a stable affirmative The value of the integral promotion after which the price will fit into the target type.
The problem here is that some values do not fit in the characters if you Use the unsigned char , instead it should work.
Lies is a bit more useful in that it warns which particular Elements generate warning and in this case it warns to 0x42 Ni does not give, but does for the rest, for example:
Error: Evaluates continuous expression 222, which can not be compressed to type 'four' ] - [0x42, 0xde, 0xad, 0xbe, 0xef}; [Required] ^ ~~~
Comments
Post a Comment