Repository navigation
Redesign eastl::variant: recursive-union storage, smallest index, compile-time visit (group G) - #88
Merged
Merged
Conversation
The reinterpret_cast implementation can't be evaluated in a constant expression, which made get_if (constexpr in std since C++17) and anything else taking the address of an object non-constant. __builtin_addressof, which MSVC STL, libc++ and libstdc++ all use, ignores an overloaded operator& the same way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… in C++20 C++20 (P2231) makes the copy and move constructors and assignments of variant constexpr when the alternatives' are. The layers call the base's ConstructFrom and AssignFrom, so they must be constexpr too; for optional, whose base functions aren't constexpr yet, nothing changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pile-time visit Storage: the alternatives live in a recursive union (like MSVC STL, libc++ and libstdc++) instead of an aligned buffer written with placement new and read with reinterpret_cast. Alternative I is reached by member accesses resolved at compile time, so variant is usable in constant expressions: construction, get, get_if, holds_alternative, visit and the comparisons in C++17, and copy, move, assignment, emplace, swap and destruction of non-trivial alternatives in C++20 (std::construct_at from <memory>, included in C++20 only). Index: the smallest signed type that holds every index, after the storage, with variant_npos as -1 (MSVC STL's encoding; libstdc++ uses the same one for up to 127 alternatives, libc++'s stable ABI uses unsigned int). index() is one sign extension; sizeof(variant<char, bool>) goes from 16 to 2, variant<int, float> from 16 to 8. Visitation: like MSVC STL, the variants' indices form one combined state and a switch on it calls the visitor with that state's alternatives, each case inlined (a jump table or a few compares once optimized). Only more than 256 combined states use a table of function pointers. Before, every call went through a table of function pointers, which MSVC rebuilt on the stack on each call, and each level of a multi-variant visit was another indirect call. Without exceptions there is no valueless state to dispatch on. visit now also accepts variants of different types and no variants at all, like std. The special members, emplace, the converting assignment, swap (including MSVC STL's swap of the object representations for trivially swappable alternatives) and the comparisons follow MSVC STL, as do the noexcept specifications. The legacy EA_DEPRECATIONS_FOR_2025_APRIL-disabled branches of variant.h are removed: variant always has the conforming behavior. natvis reads the new layout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jmgilbert2
added a commit
that referenced
this pull request
Nov 7, 2026
Brings in PRs #83 to #88: constrained unique_ptr/shared_ptr deleter constructors and any's in-place constructors, disabled hash specializations like std, compressed_pair moves its members, the C++20/23 traits, iterator, pointer_traits, span and string_view fixes of SFINAE group J, and the variant redesign (recursive-union storage, the smallest signed index, MSVC-style compile-time visit, constexpr variant). Master's test changes don't apply to this branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 8, 2026
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.
SFINAE group G, plus the visitation rewrite:
eastl::variantnow stores its alternatives in a recursive union and has the smallest index type.eastl::visitnow dispatches at compile time like MSVC STL'sstd::visit.Index type
How each of the three standard libraries does it (MSVC STL 14.51, libc++ at llvm main 635f308, libstdc++ at gcc master 5700237):
variant_nposstored asindex()signed charbelow 127 alternatives, thenshort, thenintunsigned charup to 255 alternatives, thenunsigned shortunsigned int. ABI v2 only: smallest unsigned typesize_t, before the storageAll three put the index after the storage. MSVC's encoding is the cheapest: one byte for up to 126 alternatives,
index()is a singlemovsx, and valueless is a sign test. libstdc++ does the same thing for up to 127 alternatives. EASTL now uses MSVC's encoding, after the storage. Sizes:variant<char, bool>variant<int, float>Storage
The alternatives live in a recursive union: each level holds the first remaining alternative or the union of the rest. This is how all three libraries do it. Before, EASTL used an
aligned_unionbuffer written with placement new and read withreinterpret_cast.Alternative I is reached by I member accesses that are resolved at compile time and are all at offset 0. So
get,visitand the special members never search the sub-objects at runtime, andvariantworks in constant expressions:get,get_if,holds_alternative,visitand the comparisons.emplace,swapand destruction with non-trivial alternatives. These usestd::construct_atfrom<memory>, which is included only in C++20 mode.Group G's case 191 (
constexpr variant<float, long> v(0)) now compiles.Visitation
Before: every
visitcall went through a table of function pointers. MSVC rebuilt that table on the stack on every call. Each extra variant in a multi-variant visit added another indirect call, and the call re-checked the index ineastl::get.Now it works like MSVC STL:
switchon that state calls the visitor with that state's alternatives. Each case is inlined, and the optimizer emits a jump table or a few compares.std::visit.visitnow also takes variants of different types (the old code had astatic_assertagainst that) and no variants at all, like std.Release codegen of the probe functions, as static instruction counts. Each count covers the function itself plus every out-of-line function it reaches through a call, a tail jump or a table of function pointers. Data such as jump-table entries is not counted. "Indirect calls per visit" counts the indirect calls executed for one visit; a switch's jump is not counted.
cl (MSVC 14.51):
stdvisitovervariant<int, short, long long>visitover 5 structs, calling a membervisitthat writes the alternativevisitof 2 variantsvisitof 3 variants_Visit_impl(+19 throw stubs)visit<long>visitover 20 alternativesoperator<variant<int, string>_Tidy_deallocate)clang-cl 22:
stdvisitovervariant<int, short, long long>visitover 5 structs, calling a membervisitthat writes the alternativevisitof 2 variantsvisitof 3 variantsvisit<long>visitover 20 alternativesoperator<variant<int, string>How to read these:
stdinlines the visitor bodies into its switch. Its out-of-line functions are only throw stubs (sub rsp, 40; call _Throw_bad_variant_access; int 3), one per combined state in which some variant is valueless: 1 for one variant, (3+1)² − 3² = 7 for two, 3³ − 2³ = 19 for three. They never run on valid variants.stddoesn't inline its 3-variant_Visit_implinto the caller, but the visitor bodies are inlined inside it.lea/movpairs for 5 alternatives, 20 pairs for 20.movsx, then a compare chain or jump table leads to the inlined bodies. Without exceptions there is no valueless state, so there are no throw stubs.Special members, assignment, swap, comparisons
These follow MSVC STL, including its noexcept specifications:
swap: three strategies.Also in this PR
eastl::addressofis constexpr (__builtin_addressof, as all three libraries use).get_ifneeds it.optionalareEA_CPP20_CONSTEXPR. Nothing changes foroptionaluntil its own functions become constexpr.EA_DEPRECATIONS_FOR_2025_APRIL-disabled branches of variant.h are removed (decided 2026-11-07).variantalways has the conforming behavior. optional.h's legacy branches are untouched.Tests
static_asserts for the C++17 and C++20 parts.swapstrategies.🤖 Generated with Claude Code