Skip to content

Comments

std::reference_wrapper for ModManager#707

Merged
wvpm merged 1 commit intomasterfrom
reference_wrapper_demo
Feb 23, 2026
Merged

std::reference_wrapper for ModManager#707
wvpm merged 1 commit intomasterfrom
reference_wrapper_demo

Conversation

@wvpm
Copy link
Contributor

@wvpm wvpm commented Feb 23, 2026

T* is used throughout our codebase to store pointers to objects whenever T& isn't viable due to move semantics.
For example vector_ordered_set<Mod const*>. Technically these pointers could be null and to be safe you'd have to check it isn't null before dereferencing.

Often we know the pointer may never be null. std::reference_wrapper<T> is a great way to communicate that. It is implicitly convertible to T&. This allows us to do for (Mod const& mod : loaded_mods) (with memory::vector<std::reference_wrapper<const Mod>> loaded_mods).

This approach does require the type is hashable and equatable. For types derived from HasIndex, the index is used for this. For types that don't derive from HasIndex, but do derive from HasIdentifier, the identifier is used.

@wvpm wvpm force-pushed the reference_wrapper_demo branch 2 times, most recently from 7740b19 to 1e907d9 Compare February 23, 2026 12:37
@wvpm wvpm added enhancement New feature or request topic:codestyle labels Feb 23, 2026
@wvpm wvpm marked this pull request as ready for review February 23, 2026 18:53
@wvpm wvpm changed the title std::reference_wrapper demo std::reference_wrapper for ModManager Feb 23, 2026
@wvpm wvpm enabled auto-merge February 23, 2026 18:53
@wvpm wvpm force-pushed the reference_wrapper_demo branch from 1e907d9 to 6c44fcb Compare February 23, 2026 19:44
@wvpm wvpm force-pushed the reference_wrapper_demo branch from 6c44fcb to 27edadb Compare February 23, 2026 19:48
@wvpm wvpm merged commit 43944c9 into master Feb 23, 2026
16 checks passed
@Spartan322 Spartan322 deleted the reference_wrapper_demo branch February 23, 2026 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request topic:codestyle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants