The complement of true is true, except when it's false

(dryperspective.github.io)

44 points | by aw1621107 2 days ago

6 comments

  • Panzerschrek 1 hour ago
    With such example:

      bool Complement( bool x )
      {
          return ~x;
      }
    
    GCC gives a warning if compiling with -Wall:

      test.cpp:3:10: warning: ‘~’ on an expression of type bool [-Wbool-operation]
        return ~x;
    
    So, it's surprise that C++ does this, but ones who enable compiler warnings are not in trouble.
  • rep_lodsb 6 hours ago
    They should have defined true as -1 (all bits set) instead, as in some other languages like Forth and BASIC.
    • Panzerschrek 1 hour ago
      -1 is not -1 for different sizes of integers. Which exact -1 do you want? 32-bit? 64-bit? int-sized (as int is defined in current implementation)? With current approach it's simple, boolean type is expected to have 1-bit size (which is rounded to 8 bits if stored in memory).
      • mrkeen 1 hour ago
        You have some expectations of the size of the bool (it's simply 1 bit but also 8 bits.)

        What expectations do you have of the value?

        • Panzerschrek 1 hour ago
          Bool should be logically 1-bit, when stored in memory only least significant bit should be used and the rest is allowed to be garbage. Such approach gives compilers as much room for optimizations as possible. Forcing them writing some specific bit-pattern may lead to suboptimal code generation.
  • s-zeng 3 hours ago
    In any field of characteristic 2, every element is its own negative. So on the surface this makes sense, notwithstanding the unintuitive enum corner case behaviour
    • yorwba 3 hours ago
      The article is about the complement, not the negative.
  • pwdisswordfishq 3 hours ago
    I wonder if [[=std::bitmask_type]] also enables compiler warnings that a switch statement is not exhaustive if it does not cover all possible flag combinations.
  • glownagger 2 hours ago
    I thought attributes were reserved for compiler-specific semantics like calling conventions. Here they're just being used as stropping for a language feature with syntactic significance!