cpp-docs icon indicating copy to clipboard operation
cpp-docs copied to clipboard

Improve C2135 error reference

Open Rageking8 opened this issue 4 months ago • 4 comments

// C2135.cpp

struct S
{
    int bit_field : 1;
};

int main()
{
    &S::bit_field;   // C2135
    +S::bit_field;   // C2135
    -S::bit_field;   // C2135
    !S::bit_field;   // C2135
    ~S::bit_field;   // C2135
    *S::bit_field;   // C2135
}
C:\Test>cl C2135.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35214 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

C2135.cpp
C2135.cpp(10): error C2135: 'bit_field': you cannot apply '&' to a bit-field
C2135.cpp(11): error C2135: 'bit_field': you cannot apply '+' to a bit-field
C2135.cpp(12): error C2135: 'bit_field': you cannot apply '-' to a bit-field
C2135.cpp(13): error C2135: 'bit_field': you cannot apply '!' to a bit-field
C2135.cpp(14): error C2135: 'bit_field': you cannot apply '~' to a bit-field
C2135.cpp(15): error C2135: 'bit_field': you cannot apply '*' to a bit-field

Changes to "Remarks"

  • Add 5 more operators that could emit C2135 (from my testing, there doesn't seem to be any other, but I could have missed some)
  • Add important "in this context", since the compiler either emits some other error (e.g. C2104 for S s{}; &s.bit_field;) or said operator is valid in other contexts

Changes to "Example"

  • Merge T into S for a terser example
  • Remove "address of a bit field" comment as it's a bit more nuanced than that (trying to take the address of a bit-field data member on an instance emits C2104 instead of C2135)
  • Use slightly more descriptive identifiers for clarity

Other changes

  • Update the outdated error message
  • Add C2104 "See also" link and update ms.date metadata

Rageking8 avatar Aug 13 '25 11:08 Rageking8