[Bug c++/87292] New: Warnings with Bit Fields

nunojpg at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu Sep 13 06:50:00 GMT 2018


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87292

            Bug ID: 87292
           Summary: Warnings with Bit Fields
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nunojpg at gmail dot com
  Target Milestone: ---

#include <cstdint>
#include <initializer_list>

int main()
{
    struct{
        uint8_t c1:6;
        uint8_t c2:6;
    } a;
    auto c = {a.c1, a.c2}; //warning: narrowing conversion of '(unsigned
char)c1' from 'unsigned char' to 'unsigned char:6' [-Wnarrowing]

    enum class Bool{False=0, True=1};
    struct{
        Bool v:1; //warning: 'v' is too small to hold all values of 'enum class
main()::Bool'
    } b;

    return 0;
}

I get this 2 warnings with g++, including trunk, but not with clang++.

I believe the first case is a bug, c1 is promoted to uint8_t and then is
truncated back to uint8_t:6.

The second case I am not sure if the warning is reasonable. For example clang++
will also not warn if the enum class have values defined that do not fit, and
instead will only warn if it detects a assignment that causes truncation.


More information about the Gcc-bugs mailing list