C++26: std::indirect
C++26 introduces two new vocabulary types, std::indirect and std::polymorphic, as part of P3019R14 proposal by Coe, Peacock, and Parent. These types address issues with std::unique_ptr, which breaks const propagation and deletes copy operations for composite class members. std::indirect offers value-like semantics for dynamically-allocated objects, ensuring constness propagation, copy operations, and value-based comparison and hashing.
Unlike unique_ptr, indirect provides deep copies and const-ness propagation when accessed through const access paths. Copying an indirect T copies the owned T, making the class copyable without boilerplate. Unlike unique_ptr, indirect::operator*() const returns a const T&, preventing compile errors when attempting to mutate a const object. std::indirect has no null or empty state, as it always owns an object unless moved from.
It fills a gap since C++11 by providing value semantics for indirectly-stored objects, eliminating the need for boilerplate in PIMPL implementations and enabling correct behavior in composite classes. In the following article, std::polymorphic will be explored, extending the same concept to class hierarchies.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.