std::assignable_from(3)
πΈ NAME πΈ
std::assignable_from β bestie, it's the concept that checks whether you can assign one thing to another π
β¨ SYNOPSIS β¨
Okay so first things first, this lives in the header <concepts> β that's the include you want π
template< class LHS, class RHS >
concept assignable_from =
std::is_lvalue_reference_v<LHS> &&
std::common_reference_with<
const std::remove_reference_t<LHS>&,
const std::remove_reference_t<RHS>&> &&
requires(LHS lhs, RHS&& rhs) {
{ lhs = std::forward<RHS>(rhs) } -> std::same_as<LHS>;
};
(since C++20) π
π DESCRIPTION π
So like, assignable_from<LHS, RHS> is the concept that says: an expression of the type and value category that RHS specifies can be assigned to an lvalue expression whose type is LHS. That's it, that's the vibe β¨
Like, lhs = std::forward<RHS>(rhs) has to actually work and hand you back an LHS, no cap π«Ά
π NOTES π
Not me about to tell you the most important part π Assignment does not have to be a total function. Translation for the girlies: sometimes x = y just isn't valid, and that's fine, it's not a bug.
Specifically β if assigning to some object x can cause some other object y to get modified, then x = y is likely not in the domain of =. π
When does that happen? Typically when the right operand is owned, directly or indirectly, by the left operand. For example: smart pointers to nodes in a node-based data structure, or something like std::vector<std::any>. Iconic, but also a little cursed ngl π₯
π SEE ALSO π
std::is_assignableβ checks if a type has an assignment operator for a specific argument (class template, C++11) πstd::is_trivially_assignableβ same job, checking if a type has an assignment operator for a specific argument (class template, C++11) π¦std::is_nothrow_assignableβ again with the "checks if a type has an assignment operator for a specific argument" energy (class template, C++11) π
Ok that's the whole page bestie, go assign some things π source vibes straight from http://cppreference.com, version 2024.06.10 β mwah, slay, periodt πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms