Concept Subspace :: sus :: construct :: SafelyConstructibleFromReference
template <class To, class From>concept SafelyConstructibleFromReference
requires
!__private::IsConstLvalueReference<To> ||
// If the type is the same then no temporary will be created.
std::same_as<std::remove_cvref_t<From>, std::remove_cvref_t<To>> ||
// If the type is a base class then no temporary will be created.
sus::ptr::SameOrSubclassOf<std::remove_cvref_t<From>*,
std::remove_cvref_t<To>*>
Returns whether a type From
is safely constructible from a reference of
type To
. If To
is a const reference, then the types must match, as a
conversion would create a reference to a temporary.
A struct will typically not actually store a reference but may provide an
API of references nonetheless, and then store a pointer. When used with a
universal reference From
type, this can catch the case where the incoming
reference will introduce a dangling reference when converted to the To
reference.
Examples
template <std::convertible_to<i32&> U> requires(SafelyConstructibleFromReference<i32%, U&&> void Struct::stores_i32_ref(U&& u) { ptr_ = static_cast<i32&>(&u); }