質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

2064閲覧

値のコピー省略の保証について

alphya

総合スコア124

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

3クリップ

投稿2019/01/12 23:37

編集2019/01/13 00:13

C++17で追加された機能、値のコピー省略を保証について、規格のどこにこの記述があるのかわかる方がいましたら、ご教授お願いいたします。

コンパイラの実装状況などでは、この機能についての参照にp0135r1が挙げられています。

また、n4659 [class.copy.elision]も関係しそうだと思います。[class.copy.elision]/1.1 に、次の記述があります。

in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function parameter or a variable introduced by the exception-declaration of a handler (18.3)) with the same type (ignoring cv-qualification) as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function call’s return object

(NRVOはどのように、保証されないのでしょうか)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

C++17で追加された機能、値のコピー省略を保証について、規格のどこにこの記述があるのか

私の解釈では、この文が該当するというような直接定義は存在しないと思います。

採択された提案文書 P0135R1 Wording for guaranteed copy elision through simplified value categories のタイトルが示す通り、コピー省略保証は「値カテゴリ(value categories)関連の仕様変更」によって実現されます。

大雑把には「prvalue式単体では一時オブジェクトを生成せず」、新たに「一時オブジェクトを実体化(materialize)する条件」を定義することで、結果としてコピー省略保証が実現されます。提案文書 P0135R0 より引用:

We conclude that a prvalue expression of class or array type should not create a temporary object. Instead, the temporary object is created by the context where the expression appears, if it is necessary. The contexts that require a temporary object to be created ("materialized") are as follows:
[...]

外部QAサイトStackOverflowでの質問回答 How does guaranteed copy elision work? もあわせて参考にください。


(NRVOはどのように、保証されないのでしょうか)

従来通りです。C++17 [class.copy.elision]/Paragraph 1より引用(強調部は回答者による):

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side e ects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two di erent ways of referring to the same object. If the first parameter of the selected constructor is an rvalue reference to the object’s type, the destruction of that object occurs when the target would have been destroyed; otherwise, the destruction occurs at the later of the times when the two objects would have been destroyed without the optimization. This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

投稿2019/01/13 02:28

yohhoy

総合スコア6191

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

alphya

2019/01/13 02:47

yohhoy さん、いつもご回答ありがとうございます。 なるほど、どこを探しても見つからなかったのですが、納得しました!! この度もありがとうございました!また、どうぞよろしくお願いいたします!! ☆(*>ω<*)ゞ
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問