Implement optional C++26 polyfill#176
Draft
Bronek wants to merge 17 commits into
Draft
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Rename the pfn::detail namespace-scope helpers (_storage -> _expected_base, _storage_union_t -> _expected_union_t, _is_storage_union -> _is_expected_union) so the generic names are free for the parallel pfn::optional helpers to come.
The general template mirrors pfn::detail::_expected_union_t<void, E> with the value/empty roles reversed: the engaged value lives in v_, and a trivial _dummy_t placeholder lives in e_ so the union always has an active member (for constexpr). The optional<T&> specialization is a single T* (nullptr encodes the disengaged state). Not yet wired into the optional class. Assisted-by: Claude:claude-opus-4-8
…uctors _optional_base<T> holds the flag-discriminated value union; its <T&> specialization holds the referent directly as a pointer (nullptr encodes the disengaged state, so no union, no discriminant flag, and optional<T&> stays pointer-sized and trivial). Both optional templates now privately inherit their base and drop the placeholder storage. optional<T>'s constructors and destructor are implemented over the base: default (disengaged), nullopt, in_place (and the initializer_list overload), the conditional copy/move (trivial -> defaulted, non-trivial -> the base's discriminant ctor), and a defaulted destructor. optional<T&> likewise gets its default / nullopt / in_place / copy ctors and a defaulted destructor. Converting constructors and assignment are deferred. The converting ctor placeholders are removed for now: unconstrained, optional(U&&) is an exact match for a non-const optional lvalue and would hijack the copy ctor. Assisted-by: Claude:claude-opus-4-8
Cover optional<T> and optional<T&> constructors and destructors: type aliases, triviality/noexcept/constructibility traits, constexpr construction, throwing construction, and construction-path checks via helper_t::state. Observers, assignment, swap and monadic operations are not yet defined, so the tests are compile-time-focused and avoid ODR-using them. Assisted-by: Claude:claude-opus-4-8 Assisted-by: Augment:auggie-0.31.0
The move ctor's conditional noexcept is standard-mandated ([optional.optional]), not a pfn extension like the copy ctor's; the sibling sections already leave it unconditional. Assisted-by: Claude:claude-sonnet-5
…tional Mirrors expected_validation.cpp's nested-include pattern, but runs unconditionally (no LIBFN_MODE gate) since std::optional predates C++23. The reference specialization is excluded: no released standard library implements C++26's optional<T&> yet. Assisted-by: Claude:claude-sonnet-5
optional<T>'s copy/move assignment reuse _optional_union_t::_reinit the same way _expected_base::_assign already does for expected<void, E>: one side of the transition is always the trivial _dummy_t, so no strong- exception-guarantee snapshot of the old member is needed. _optional_base gains _reset() (the shared disengage step used by operator=(nullopt_t) and emplace) and _assign(), and optional<T> wires up operator=(nullopt_t), operator=(optional const&) and operator=(optional&&) over them. Converting assignment stays deferred, same as the converting constructors. emplace() and the core observers (has_value, operator bool, operator*, operator->) are implemented in _optional_base and re-exposed via `using`, matching how expected already does it. Observers were pulled forward from their own [optional.observe] slice because assignment tests need them to verify anything past compile-time traits; value()/value_or() stay declared-only since they need a bad_optional_access type. Tests mirror expected.cpp's assignment/emplace shape (same helper_t<V> fixture naming, from-rval/from-lval-const nesting, constexpr sections). Verified against both pfn and, via optional_validation.cpp, real std::optional on libstdc++ and libc++ under ASan+UBSan+LeakSan; the run surfaced a genuine libstdc++/libc++ disagreement over whether emplace is conditionally noexcept (unspecified by the standard), gated accordingly. Assisted-by: Claude:claude-sonnet-5
optional<T&> is pointer-like: T* is trivial, so unlike optional<T> none of this needs _reinit/_assign/_reset. operator=(nullopt_t) just clears the pointer. emplace(U&&) is constrained on is_constructible_v<T&, U> and implemented via _convert_ref_init_val, the [optional.ref.expos] helper that binds a reference through whatever conversion T& requires and stores its address; the dangling-reference guard from [optional.ref.assign]'s constraints (reference_constructs_from_temporary_v) is left as a TODO, since it's a C++23-only trait with no portable C++20 fallback -- the same gap the existing in_place ctor already has. The observers (has_value, operator bool, operator*, operator->) each get a single overload: const does not propagate to the referent here, so a const optional<T&> still yields a mutable T&, unlike optional<T>'s const/ref-qualified overload set. Tests cover the pointer-rebind semantics explicitly (assignment and emplace rebind rather than assign through the old referent) and the const-non-propagation property via static_assert on decltype. Assisted-by: Claude:claude-sonnet-5
Construction and assignment from optional<U> read the source through its public has_value()/operator* rather than expected's friend-cast into the private base: optional<U&> sources have a differently-shaped base, and a helper returning the base by value gets no copy elision in a delegating constructor's mem-initializer, so a _from_optional_t tag constructor constructs the union member in place instead. Assisted-by: Claude:claude-sonnet-5
Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-fable-5
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Unsure if this PR will also include range support