[過去ログ] C++相談室 part155 (1002レス)
前次1-
抽出解除 必死チェッカー(本家) (べ) 自ID レス栞 あぼーん

このスレッドは過去ログ倉庫に格納されています。
次スレ検索 歴削→次スレ 栞削→次スレ 過去ログメニュー
リロード規制です。10分ほどで解除するので、他のブラウザへ避難してください。
145: 2021/04/09(金)12:20 ID:QYkH8yRN(1/4) AAS
>>143
以下のサイトによれば、「標準的な仕様は決まっている」ようです:
drafet C++11 standareのsection 12.8のparagraph 15に
implicitly-defined copy/move constructor は、
「a memberwise copy/move of its bases and members」
であると書いてあるそうですから:

外部リンク:stackoverflow.com

The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members. [ Note: brace-or-equal-initializers of non-static data members are ignored. See also the example in 12.6.2. —end note ] The order of initialization is the same as the order of initialization of bases and members in a user-defined constructor (see 12.6.2). Let x be either the parameter of the constructor or, for the move constructor, an xvalue referring to the parameter. Each base or non-static data member is copied/moved in the manner appropriate to its type:

if the member is an array, each element is direct-initialized with the corresponding subobject of x;
if a member m has rvalue reference type T&&, it is direct-initialized with static_cast(x.m);
省2
146
(1): 2021/04/09(金)12:39 ID:QYkH8yRN(2/4) AAS
>>142
>ムーブセマンティクス一般においてはムーブ後の抜け殻は「無効なオブジェクト」なのでアクセスに対して保証がないことがあるが、
>標準ライブラリのスマートポインタについては所有権を移動させた後にそれが空であり
>メンバ関数 get で nullptr が返ることも、 nullptr と == で比較して真であることも保証される。

なるほど。
行(1)の場合、
1. 右辺でstd::move()をt1に行った段階では(実行段階で)マシン語は全く実行されない。
2. 中央の = は、move-constructor と解釈され、Tのmove-constructorが呼び出される。
3. Tの暗黙定義のmove-constructorは、メンバ同士のmove-construcotrなので、
 メンバに shared_ptr<A> a があると、shared_ptr<A>のmoveコンストラクタが呼び出される。
省3
147
(1): 2021/04/09(金)12:42 ID:QYkH8yRN(3/4) AAS
>>146
逆に言えば、Tのメンバに、A *pA; のような生ポインタがあると、
Tの暗黙のmoveコンストラクタ/move代入演算子では、恐らく、
「src側(from側)」にはnullptrが代入されない(??)ので、非常に困った
問題を招くかも知れないと?
148
(1): 2021/04/09(金)12:44 ID:QYkH8yRN(4/4) AAS
>>147
Tのデストラクタに
if ( pA != nullptr ) {
 delete pA;
}
などと書いていた場合、問題を生じそうですね。
前次1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.025s