Search code examples
c++type-conversionimplicit-conversionnarrowing

Hidden narrowing conversion from int to uint8_t


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 chars. 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?


Solution

  • You were close with this:

    uint8_t mask{static_cast<uint8_t>(a.get_a()) | static_cast<uint8_t>(aa.get_a())};
    

    But a.get_a() and aa.get_a() are already uint8_t, so the cast does nothing.

    It's the | operation that:

    • converts both operands to int (after you can do anything about it)
    • evaluates to an int

    So it's the whole expression you now need to subsequently convert:

    uint8_t mask{static_cast<uint8_t>(a.get_a() | aa.get_a())};
    

    You were also right to try dropping the {}-initialisation, which is probably what I'd do too. You just don't need its strictness here.

    uint8_t mask = a.get_a() | aa.get_a();
    

    This is clear, concise and correct.