Consider the following piece of code:
#include <cstdint>
class A
{
public:
explicit A(uint8_t p_a){ m_a = p_a; };
uint8_t get_a() const {return m_a;}
private:
uint8_t m_a;
};
int main()
{
A a {0x21U};
A aa{0x55U};
uint8_t mask{a.get_a() | aa.get_a()};
return 0;
}
When I try to compile this (gcc 5.4.0
) I get the following error:
main.cpp: In function ‘int main()’:
main.cpp:20:28: warning: narrowing conversion of ‘(int)(a.A::get_a() | aa.A::get_a())’ from ‘int’ to ‘uint8_t {aka unsigned char}’ inside { } [-Wnarrowing]
uint8_t mask{a.get_a() | aa.get_a()};
I don't really understand why there is any narrowing at all. The int
type is never used anywhere in my code, everything is written in terms of unsigned char
s. Even if I explicitly cast to unsigned char
I get the error:
uint8_t mask{static_cast<uint8_t>(a.get_a()) | static_cast<uint8_t>(aa.get_a())};
In fact, to solve this, I need to remove the {}
-initialization. Then it works:
uint8_t mask = a.get_a() | aa.get_a();
Why is this necessary?