cplusplus / cplusplus/draft

[class.bit] p4 It is not clear whether a value of enumeration whose underlying type is fixed can be compared equally with the stored value in a bit-field

Open
#5,200 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TeX
Stars
221
Forks
813
Avg merge
16h 4m
Merged PRs (30d)
36

Description

[class.bit] p4 states

If a value of an enumeration type is stored into a bit-field of the same type and the width is large enough to hold all the values of that enumeration type ([dcl.enum]), the original value and the value of the bit-field compare equal.

Presumably, this provision should cover the complete cases of the enumeration type. Instead, The example that follows this provision only demonstrates the one whose underlying type is not fixed.

enum BOOL { FALSE=0, TRUE=1 };
struct A {
  BOOL b:1;
};
A a;
void f() {
  a.b = TRUE;
  if (a.b == TRUE)              // yields true
    { /* ... */ }
}

For the enumeration BOOL whose underlying type is not fixed, [dcl.enum] p8 indeed specifies the smallest width the bit-field that can hold all the values should be.

For an enumeration whose underlying type is fixed, the values of the enumeration are the values of the underlying type. Otherwise, the values of the enumeration are the values representable by a hypothetical integer type with minimal width M such that all enumerators can be represented. The width of the smallest bit-field large enough to hold all the values of the enumeration type is M.

However, how about the enumeration type whose underlying type is fixed? Consider such two examples:

Clang has a warning report for this example

enum BOOL:  int { 
    FALSE=0, 
    TRUE=1 
};
struct A{
    BOOL b: 1;
};
int main(){
   A a;
   a.b = TRUE;
}

Instead, this example seems to be ok without any warning information

enum BOOL: unsigned int { 
    FALSE=0, 
    TRUE=1 
};
struct A{
    BOOL b: 1;
};
int main(){
   A a;
   a.b = TRUE;
}

The same point that these two examples both have is that the enumeration BOOL whose underlying type is fixed, the different point is that their underlying types have a different signedness.

Seems we lack to specify a case whether a value of an enumeration type whose underlying type is fixed stored into a bit-field with width N can be compared equally with the original value. Should we augment [class.bit] p4 to be that?

If a value of an enumeration type whose underlying type is fixed is stored into a bit-field with width N of the same type and the value would be representable in a hypothetical signed or unsigned integer type with width N and the same signedness as the underlying type of the enumeration, the original value and the value of the bit-field compare equal. If a value of an enumeration type whose underlying type is not fixed is stored into a bit-field with width N of the same type and the width N is large enough to hold all the values of that enumeration type ([dcl.enum]), the original value and the value of the bit-field compare equal.

The above augment seems to be more conform to what the implementations have done.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read [class.bit] p4 and [dcl.enum] p8, then compare the fixed-underlying-type and non-fixed-underlying-type examples described in the issue. Determine whether the proposed wording accurately specifies comparison behavior for both signedness cases; done means the standard text and examples clearly resolve the reported ambiguity.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.