std::assignable_from
来自 cppreference.cn
定义于头文件 <concepts> |
||
template< class LHS, class RHS > concept assignable_from = |
(自 C++20) | |
概念 assignable_from<LHS, RHS>
指定了由 RHS
指定的类型和值类别的表达式可以赋值给由 LHS
指定类型的左值表达式。
内容 |
[编辑] 语义要求
给定
-
lhs
,一个左值,它引用一个对象lcopy
,使得 decltype((lhs)) 是LHS
, -
rhs
,一个表达式,使得 decltype((rhs)) 是RHS
, -
rcopy
,一个与rhs
相等的不同对象,
仅当满足以下条件时,assignable_from<LHS, RHS>
才会被建模
- std::addressof(lhs = rhs) == std::addressof(lcopy) (即,赋值表达式产生一个引用左操作数的左值);
- 在评估 lhs = rhs 之后
-
lhs
等于rcopy
,除非rhs
是引用lcopy
的非常量 xvalue(即,赋值是自移动赋值), - 如果
rhs
是一个泛左值- 如果它是一个非常量 xvalue,则它引用的对象处于有效但不确定的状态;
- 否则,它引用的对象不会被修改;
-
[编辑] 相等性保持
标准库概念的 requires 表达式 中声明的表达式需要是 相等性保持 的(除非另有说明)。
[编辑] 注释
赋值不需要是全函数。 特别是,如果赋值给某个对象 x
可能导致修改另一个对象 y
,则 x = y 很可能不在 =
的域中。 如果右操作数直接或间接地由左操作数拥有(例如,使用指向基于节点的数据结构中的节点的智能指针,或使用类似 std::vector<std::any>)时,通常会发生这种情况。
[编辑] 示例
运行此代码
#include <atomic> #include <concepts> #include <string> int main() { // Normal basic usage, checks lvalue reference assignment static_assert(std::is_assignable_v<int&, int>); static_assert(std::assignable_from<int&, int>); static_assert(std::is_assignable_v<std::string&, std::string>); static_assert(std::assignable_from<std::string&, std::string>); // Fundamental types don't support assignment to an rvalue static_assert(!std::is_assignable_v<int, int>); static_assert(!std::assignable_from<int, int>); // std::assignable_from doesn't accept all valid assignment expressions: // rvalue reference assignment static_assert(std::is_assignable_v<std::string&&, std::string>); static_assert(!std::assignable_from<std::string&&, std::string>); // rvalue assignment static_assert(std::is_assignable_v<std::string, std::string>); static_assert(!std::assignable_from<std::string, std::string>); // std::atomic::operator= returns by value static_assert(std::is_assignable_v<std::atomic<int>&, int>); static_assert(!std::assignable_from<std::atomic<int>&, int>); }
[编辑] 参考
- C++23 标准 (ISO/IEC 14882:2024)
- 18.4.8 概念
assignable_from
[concept.assignable]
- 18.4.8 概念
- C++20 标准 (ISO/IEC 14882:2020)
- 18.4.8 概念
assignable_from
[concept.assignable]
- 18.4.8 概念
[编辑] 参见
(C++11)(C++11)(C++11) |
检查类型是否具有特定参数的赋值运算符 (类模板) |