Skip to content

ui::ItemKey

Header: winui/core/ObservableVector.hpp.

template <typename T> class ItemKey

What makes two items of a collection "the same item" when the collection is assigned as a whole: the type of ui::ForEach's .key field, and what ui::item_count() takes for a view.

Made from a pointer to a data member (.key = &Photo::id) or from any callable returning the key (.key = [](const Photo& p) { return p.path; }). The key type needs == and std::hash. Keys must be unique within the collection.

ItemKey

ItemKey() = default

No key: items are matched by position and value.

ItemKey

template <typename F>
ItemKey(F key)
    requires std::invocable<const F&, const T&> && std::copy_constructible<F> && std::equality_comparable<std::remove_cvref_t<std::invoke_result_t<const F&, const T&>>> && requires(const std::remove_cvref_t<std::invoke_result_t<const F&, const T&>>& k) { std::hash<std::remove_cvref_t<std::invoke_result_t<const F&, const T&>>>{}(k); }

A key read from each item by key: a member pointer or a callable.

operator bool

operator bool() const

True when a key is set.

match

bool match(const std::vector<T>& old, const std::vector<T>& next, std::vector<std::optional<std::size_t>>& source, std::vector<bool>& changed)

Matches next against old by key. For each item of next, source receives the position of the item of old with the same key, unset for a new one, and changed whether a matched item differs in value. False when old holds a key twice, or when two items of next match the same item of old. A key repeated only among the new items of next is not detected.