This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.

4493. Specification for some functions of bit reference types seems missing

Section: 22.9.2.1 [template.bitset.general], 23.3.14.1 [vector.bool.pspc] Status: New Submitter: Jiang An Opened: 2025-12-16 Last modified: 2025-12-20

Priority: Not Prioritized

View all other issues in [template.bitset.general].

View all issues with New status.

Discussion:

We haven't explicitly specified the return values of bitset<N>::reference::operator bool() and vector<bool, A>::reference::operator bool(), although the intended return values can be inferred from "a class that simulates a reference to a single bit in the sequence". Moreover, specification for bitset<N>::reference::operator~ seems completely missing, and the comment "flip the bit" seems misleading. Implementations consistently make the operator~ return !operator bool().

Proposed resolution:

This wording is relative to N5032.

  1. Modify 22.9.2.1 [template.bitset.general] as indicated:

    […]
    // bit reference
    class reference {
    public:
      constexpr reference(const reference& x) noexcept;
      constexpr ~reference();
      constexpr reference& operator=(bool x) noexcept; // for b[i] = x;
      constexpr reference& operator=(const reference& x) noexcept; // for b[i] = b[j];
      constexpr const reference& operator=(bool x) const noexcept;
      constexpr operator bool() const noexcept; // for x = b[i];
      constexpr bool operator~() const noexcept; // flips the bit
      constexpr reference& flip() noexcept; // for b[i].flip();
      
      friend constexpr void swap(reference x, reference y) noexcept;
      friend constexpr void swap(reference x, bool& y) noexcept;
      friend constexpr void swap(bool& x, reference y) noexcept;
    };
    […]
    
    […]
    constexpr reference& reference::operator=(bool x) noexcept;
    constexpr reference& reference::operator=(const reference& x) noexcept;
    constexpr const reference& reference::operator=(bool x) const noexcept;
    

    -6- Effects: Sets the bit referred to by *this if bool(x) is true, and clears it otherwise.

    -7- Returns: *this.

    constexpr reference::operator bool() const noexcept;
    

    -?- Returns: true if the value of the referred to bit is one, false otherwise.

    constexpr bool reference::operator~() const noexcept;
    

    -?- Returns: !bool(*this).

  2. Modify 23.3.14.1 [vector.bool.pspc] as indicated:

    […]
    constexpr reference& reference::operator=(bool x) noexcept;
    constexpr reference& reference::operator=(const reference& x) noexcept;
    constexpr const reference& reference::operator=(bool x) const noexcept;
    

    -7- Effects: Sets the bit referred to by *this when bool(x) is true, and clears it otherwise.

    -8- Returns: *this.

    constexpr reference::operator bool() const noexcept;
    

    -?- Returns: true if the value of the referred to bit is one, false otherwise.