Skip to content

Fix: make pcg_extras.hpp self-contained for PCG_NODISCARD - #18

Merged
JkarVN merged 1 commit into
masterfrom
fix/nodiscard-self-contained
Dec 2, 2026
Merged

JkarVN merged 1 commit into
masterfrom
fix/nodiscard-self-contained

Conversation

@JkarVN

@JkarVN JkarVN commented Dec 2, 2026

Copy link
Copy Markdown
Member

Problem

pcg_extras.hpp uses PCG_NODISCARD (on generate_one and bounded_rand) but the macro was only ever defined in pcg_random.hpp. As a result the header no longer compiles when included on its own:

pcg_extras.hpp:544:1: error: unknown type name 'PCG_NODISCARD'
  PCG_NODISCARD inline UInt generate_one(SeedSeq&& generator)
^

This is a regression: PCG_NODISCARD was introduced for these two functions in a94acdf ("Modernize codebase"), and before that commit pcg_extras.hpp had zero usages of the macro and compiled standalone without issue. 178532b ("Correct PCG_NODISCARD placement") did not address it.

It only works by accident today because pcg_random.hpp defines the macro on line 114 and includes pcg_extras.hpp on line 133. Anyone including <pcg_extras.hpp> directly — e.g. to use bounded_rand, seed_seq_from or generate_one — hits the error immediately. This also affects downstream projects that vendor the header individually (SLiM ships eidos/pcg_extras.hpp and eidos/pcg_random.hpp as two separate files).

Change

  • Define PCG_NODISCARD in pcg_extras.hpp under #ifndef PCG_NODISCARD, so the header is self-contained.
  • Wrap the existing definition in pcg_random.hpp in the same guard, so neither include order produces a redefinition warning.
  • Drop a duplicated #include <limits> in pcg_extras.hpp.

Header-only API and all generator output are unchanged.

Verification

Each header compiled standalone (-I include with only that header included):

Header Before After
pcg_random.hpp OK OK
pcg_uint128.hpp OK OK
pcg_extras.hpp compile error OK
  • Both include orders (extras -> random and random -> extras) compile with no redefinition warning.
  • clang and MSVC, /std:c++14 and /std:c++17, -Wall -Wextra -Wpedantic: clean.
  • ctest: 42/42 passed.

pcg_extras.hpp uses PCG_NODISCARD (generate_one, bounded_rand) but the macro was only defined in pcg_random.hpp, so including <pcg_extras.hpp> on its own failed with "unknown type name PCG_NODISCARD". Regression introduced when PCG_NODISCARD was added in a94acdf; the header compiled standalone before that.

Define PCG_NODISCARD in pcg_extras.hpp under an #ifndef guard, and guard the existing definition in pcg_random.hpp so both orders work without a redefinition warning.

Also drop a duplicated #include <limits> in pcg_extras.hpp.

Verified: all three headers compile standalone with clang and MSVC at -std=c++14/17/20 (-Wall -Wextra -Wpedantic); ctest 42/42.
@JkarVN
JkarVN merged commit 7d3b4bc into master Dec 2, 2026
9 checks passed
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.

2 participants