Skip to content

Fix randen_engine default constructor overload - #2501

Open
iwang-1 wants to merge 1 commit into
abseil:masterfrom
iwang-1:randen-engine-default-ctor
Open

iwang-1 wants to merge 1 commit into
abseil:masterfrom
iwang-1:randen-engine-default-ctor

Conversation

@iwang-1

@iwang-1 iwang-1 commented Nov 13, 2026

Copy link
Copy Markdown

randen_engine() delegates with randen_engine(0). The literal 0 is an int, so overload resolution picks the forwarding template <class SeedSequence> randen_engine(SeedSequence&&) constructor (an exact match for int) instead of explicit randen_engine(result_type). That constructor then calls seed(seq) with an int, which converts to the unsigned result_type and triggers -Wsign-conversion (part of -Wconversion in ABSL_LLVM_FLAGS).

Any translation unit that default-constructs a randen_engine and compiles with Abseil's LLVM flags plus -Werror fails to build. In this repository that includes absl/random/internal/nanobenchmark.cc and absl/random/internal/randen_benchmarks.cc:

absl/random/internal/randen_engine.h:72:10: error: implicit conversion changes signedness: 'int' to 'result_type' (aka 'unsigned int') [-Werror,-Wsign-conversion]
   72 |     seed(seq);
absl/random/internal/randen_engine.h:65:21: note: in instantiation of function template specialization 'absl::random_internal::randen_engine<unsigned int>::randen_engine<int, void>' requested here
   65 |   randen_engine() : randen_engine(0) {}

This change passes result_type{0}, so the default constructor delegates to randen_engine(result_type) as intended. Both paths end in seed(0), so the engine's output is unchanged.

Testing: on macOS (Apple clang 17, arm64), a program that default-constructs randen_engine<uint32_t> and randen_engine<uint64_t> failed to compile with ABSL_LLVM_FLAGS and -Werror=sign-conversion before this change and compiles after it. In both versions, the first 1000 outputs of a default-constructed engine match an engine constructed with 0u.

🤖 Generated with Claude Code

randen_engine() delegated with randen_engine(0). The int literal is an
exact match for the forwarding SeedSequence&& constructor, so it was
chosen over randen_engine(result_type), and its seed(seq) call then
converted int to the unsigned result_type. That triggers
-Wsign-conversion, which breaks builds of code that default-constructs
the engine with ABSL_LLVM_FLAGS and -Werror.

Pass result_type{0} so the intended constructor is used. Both paths end
in seed(0), so the output is unchanged.
@google-cla

google-cla Bot commented Nov 13, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@iwang-1
iwang-1 force-pushed the randen-engine-default-ctor branch from f24fec5 to b2d3830 Compare November 18, 2026 23:01
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