Problem
I am trying to create a map of std::uint8_t
-> char
, and initialise it with some values:
const std::map<std::uint8_t, char> ScenarioReader::alphabet = {
{ 0x12, 'b' },
{ 0x13, 'c' },
{ 0x15, 'a' },
{ 0x16, 'f' },
...
}
This generates a compiler warning because these integer literals (0x12
, etc.) are recognised as unsigned ints
, which are larger than a std::uint8_t
:
1>d:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.16.27023\include\utility(172): warning C4244: 'initializing': conversion from '_Ty' to '_Ty1', possible loss of data
1> with
1> [
1> _Ty=unsigned int
1> ]
1> and
1> [
1> _Ty1=uint8_t
1> ]
1>d:\my-project\src\myfile.cpp(75): note: see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<unsigned int,char,0>(_Other1 &&,_Other2 &&) noexcept' being compiled
1> with
1> [
1> _Kty=uint8_t,
1> _Ty=char,
1> _Other1=unsigned int,
1> _Other2=char
1> ]
1>d:\my-project\src\myfile.cpp(12): note: see reference to function template instantiation 'std::pair<const _Kty,_Ty>::pair<unsigned int,char,0>(_Other1 &&,_Other2 &&) noexcept' being compiled
1> with
1> [
1> _Kty=uint8_t,
1> _Ty=char,
1> _Other1=unsigned int,
1> _Other2=char
1> ]
Solutions
I am aware of 2 possible ways to fix this:
1) Disable warnings for this section
#pragma warning( push )
#pragma warning( disable : 4244 )
const std::map<std::uint8_t, char> ScenarioReader::alphabet = {
{ 0x12, 'b' },
{ 0x13, 'c' },
{ 0x15, 'a' },
{ 0x16, 'f' },
...
}
#pragma warning( pop)
2) Cast every key explicitly
const std::map<std::uint8_t, char> ScenarioReader::alphabet = {
{ static_cast<std::uint8_t>(0x12), 'b' },
{ static_cast<std::uint8_t>(0x13), 'c' },
{ static_cast<std::uint8_t>(0x15), 'a' },
{ static_cast<std::uint8_t>(0x16), 'f' },
...
}
I am not particularly happy about either approach, but the second is especially ugly to me.
Am I, perhaps, missing a simpler solution?