Skip to content

fix: make OptionalReference::emplace bind the supplied reference - #228

Merged
alexkaratarakis merged 1 commit into
teslamotors:mainfrom
rioyu123:codex/fix-optional-reference-emplace
Sep 24, 2026
Merged

alexkaratarakis merged 1 commit into
teslamotors:mainfrom
rioyu123:codex/fix-optional-reference-emplace

Conversation

@rioyu123

@rioyu123 rioyu123 commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Summary

OptionalReference::emplace() fails to compile even when passed a simple int lvalue. Its val parameter hides the private val() accessor, so the implementation tries to call the integer as a function.

The fix calls this->val() explicitly and uses std::addressof to bind the supplied object, just like the existing reference constructor. Rebinding leaves the previous object untouched and does not invoke an overloaded address-of operator.

The regression covers first binding, rebinding and returned-reference aliasing, const and constant-evaluated use, and a type with an overloaded operator&.

This is separate from #221, which adds and_then, transform, and or_else but leaves emplace() unchanged.

Testing

Validated on Linux with GCC 13.3 and C++20 in a Debug build using the repository's unmodified top-level CMake project and an isolated CMake CONFIG dependency prefix.

  • The final regression fails to compile against unchanged main (d0df5f67e102ba42025ef0ecf761d19a4510a543) at the shadowed val() call, including both constant-evaluated cases.
  • cmake --build build --target optional_reference_test --parallel 4: passed with the repository's -Werror and -fno-exceptions settings.
  • ctest --test-dir build --build-config Debug -R '^optional_reference_test$' --output-on-failure: 1/1 target passed, covering all 29 GoogleTest cases in the binary.
  • Changed-lines check with clang-format 21.1.8 and git diff --check passed.

Other repository targets, compilers, sanitizers, and platforms were not run for this candidate.

@alexkaratarakis
alexkaratarakis force-pushed the codex/fix-optional-reference-emplace branch from ebe44e5 to f676f5d Compare September 22, 2026 03:45
@alexkaratarakis
alexkaratarakis merged commit f676f5d into teslamotors:main Sep 24, 2026
5 checks passed
@alexkaratarakis

Copy link
Copy Markdown
Collaborator

Thank you!
I additionally renamed the parameter to further avoid ambiguity.

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