Skip to content

Tighten absl::BitGenRef binding rules and simplify its implementation. - #2294

Draft
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1004460719
Draft

copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1004460719

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Tighten absl::BitGenRef binding rules and simplify its implementation.

  • Reject binding to const generators. Generating a variate mutates the
    generator, but BitGenRef previously accepted const URBG& and later
    invoked the non-const operator() through a cast, which is undefined
    behavior for generators defined const. Such constructions now resolve to
    a deleted constructor.
  • Detect operator BitGenRef() on an lvalue rather than an rvalue. This
    lets types ref-qualify the operator (operator BitGenRef() &) to
    prevent binding a BitGenRef to a temporary; previously such types were
    silently wrapped as a URBG instead, adding a layer of indirection.
  • Merge the mocking and non-mocking constructors; the mock dispatch
    function is now selected with if constexpr.
  • Store the generator as void* instead of uintptr_t, replacing
    reinterpret_casts with static_casts, and use a null mock function
    pointer instead of a NotAMock sentinel.

* Reject binding to const generators. Generating a variate mutates the
  generator, but BitGenRef previously accepted `const URBG&` and later
  invoked the non-const `operator()` through a cast, which is undefined
  behavior for generators defined const. Such constructions now resolve to
  a deleted constructor.
* Detect `operator BitGenRef()` on an lvalue rather than an rvalue. This
  lets types ref-qualify the operator (`operator BitGenRef() &`) to
  prevent binding a BitGenRef to a temporary; previously such types were
  silently wrapped as a URBG instead, adding a layer of indirection.
* Merge the mocking and non-mocking constructors; the mock dispatch
  function is now selected with `if constexpr`.
* Store the generator as `void*` instead of `uintptr_t`, replacing
  reinterpret_casts with static_casts, and use a null mock function
  pointer instead of a `NotAMock` sentinel.

PiperOrigin-RevId: 1004460719
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant