std::assignable_from

来自cppreference.com
< cpp‎ | concepts
在标头 <concepts> 定义
template<class LHS, class RHS >

concept assignable_from =
    std::is_lvalue_reference_v<LHS>&&
    std::common_reference_with<
        conststd::remove_reference_t<LHS>&,
        conststd::remove_reference_t<RHS>&>&&
    requires(LHS lhs, RHS&& rhs){
        { lhs =std::forward<RHS>(rhs)}->std::same_as<LHS>;

    };
(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 的非 const 亡值(即赋值为自移动赋值),
    • rhs 是泛左值;
      • 若它是非 const 亡值,则其所指代的对象在合法但未指定的状态;
      • 否则,不修改其所指代的对象;

[编辑]相等性保持

标准库概念的 requires 表达式中声明的表达式都要求保持相等性(除非另外说明)。

[编辑]注解

赋值不必为全函数。具体来说,若赋值给某对象 x 可能导致修改某其他对象 y,则 x = y 可能不在 = 的定义域中。若左操作数直接或间接占有右操作数(例如用指向基于结点的数据结构中的结点的智能指针,或用类似 std::vector<std::any> 的构造),则这常发生。

[编辑]示例

#include <atomic>#include <concepts>#include <string>   int main(){// 普通的基本用法,检查左值引用赋值 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>);   // 基础类型不支持向右值赋值 static_assert(!std::is_assignable_v<int, int>); static_assert(!std::assignable_from<int, int>);   // std::assignable_from 并不接受全部的有效赋值表达式:   // 右值引用赋值 static_assert(std::is_assignable_v<std::string&&, std::string>); static_assert(!std::assignable_from<std::string&&, std::string>);   // 右值赋值 static_assert(std::is_assignable_v<std::string, std::string>); static_assert(!std::assignable_from<std::string, std::string>);   // std::atomic::operator= 按值返回 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 Concept assignable_from [concept.assignable]
  • C++20 标准(ISO/IEC 14882:2020):
  • 18.4.8 Concept assignable_from [concept.assignable]

[编辑]参阅

检查类型是否有针对特定实参的赋值运算符
(类模板)[编辑]
close