std:: assignable_from
|
定義先ヘッダ
<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を参照する非const xvalueである場合(すなわち自己ムーブ代入)を除く; -
rhsがglvalueの場合:- 非const xvalueの場合、それが参照するオブジェクトは有効だが未規定の状態となる;
- それ以外の場合、参照先オブジェクトは変更されない;
-
等価性保存
標準ライブラリコンセプトの requires 式 で宣言される式は、 equality-preserving であることが要求されます(特に明記されている場合を除く)。
注記
代入は全関数である必要はありません。特に、何らかのオブジェクト
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 コンセプト
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)
|
型が特定の引数に対する代入演算子を持つかどうかをチェックする
(クラステンプレート) |