LoginSignup
11
9

More than 5 years have passed since last update.

p0052をのんびり和訳していく

Last updated at Posted at 2015-12-11

まだ訳し始めたばかりです!!

のんびりp0052r0を訳していたらp0052r1が出てしまったでござるp0052r2まで出てきたいつの間にかp0052r8まで行っていた・・・。
https://groups.google.com/a/isocpp.org/forum/#!topic/std-proposals/b74TFYOWRB8

初めに

p0052とは

C++erなら江添氏のブログをすべて目を通すのは常識なので、すでにご存知かと思いますが
http://cpplover.blogspot.jp/2016/02/c-p0033r1-p0059r1.html
http://cpplover.blogspot.jp/2015/11/c-p0050r0-p0059r0.html
で紹介されていますね。

要するにライブラリ版のfinallyであるscope_exitと汎用RAIIラッパーであるunique_resourceを標準に追加する提案です。

こんな感じにバージョンアップしています。C++17に入らなかったので順調にC++17を踏まえた修正が入っているようです。

ちなみにscope_exit自体はBoostにもあるようですがそれとはすこし違うようです

翻訳方針と注意事項

今回は諸事情ありscope_exit周辺の翻訳を優先していきます。

機械翻訳のほうがまし、とか言わない。

一週間くらいでscope_exitの訳が終わると良いなぁ(大変そう)

訳せないところはとりあえず英語のままにしておく。下手な日本語訳よりはマシのはず

P0052 - Generic Scope Guard and RAII Wrapper for the Standard Library

Peter Sommerlad and Andrew L. Sandoval
2015-09-27

Document Number:p0052r8 (update of N4189, N3949, N3830, N3677)
Date 2018-04-24
Project Programming Language C++
Audience LWG/LEWG

1.歴史

1.7.P0052R1からの変更点

The Jacksonville LEWG, especially Eric Niebler gave splendid input in how to improve the classes in this paper. I (Peter) follow Eric’s design in specifying scope_exit as well as unique_resource in a more general way.

  • Provide scope_fail and scope_success as classes. However, we may even hide all of the scope guard types and just provide the factories.
  • safe guard all classes against construction errors, i.e., failing to copy the deleter/exit-function, by calling the passed argument in the case of an exception, except for scope_success.
  • relax the requirements for the template arguments.

Special thanks go to Eric Niebler for providing several incarnations of an implementation that removed previous restrictions on template arguments in an exception-safe way (Eric: "This is HARD."). To cite Eric again: "Great care must be taken when move-constructing or move-assigning unique_resource objects to ensure that there is always exactly one object that owns the resource and is in a valid, Destructible state." Also thanks to Axel Naumann for presenting in Jacksonville and to Axel, Eric, and Daniel Krügler for their terrific work on wording improvements.

1.8.P0052R0からの変更点

Kona会議(2015/10)では多くの意見が得られた。

(訳注:nothrow copy constructibleであるか、nothrow move constructibleであるか、referenceを持つのかで特殊化はめんdなので)リソースとデリーターの型はnothrow-copyableを満たすもののみにすることで、コンストラクターと文面をシンプルに、という意見が際立っていた。

referenceを要求する場合、コンストラクターや文面は複雑になり、ユーザーはmake系関数に渡すのにstd::ref/std::crefを使うようになってしまう

  • nothrow copyableな型に制限することでコンストラクタと文面をシンプルに。本当にシンプルな型を想定しています。この変更は型消去をするstd::functionオブジェクトをデリーターとして指定した時、コピー時に例外を投げる可能性があるという問題を回避します。
  • 動機(motivation)の項を文面に復活させた。レビューをしやすくして、特定APIの問題への理由を提供する
  • Make "Alexandrescu's" "declarative" scope exit variation employing uncaught_exceptions() counter optional factories to chose or not.
  • scopreヘッダーを対応するテーブルに追加し、スタンドアロンで利用できるように
  • 編集上の微調整
  • unique_resourceのoperator*を再び追加
  • リソースをreference_wrapperとして受け取れるmake_unique_resourceのオーバーロードを追加。
    reference_wrapper自体がoperator()を提供しているので、デリーター関数がreference_wrapperを介して来ることを想定したオーバーロードは必要ないです。

1.9.N4189からの変更点

  • Cologne会議(2015/02)で指摘された文面の問題をLWGに提出するために修正を試みる(Ville Voutilainenから知らされたのが、締め切りの一週間前だったので、すべてを反映出来ていません)
    • 終了時に実行される関数がnothrow copy constructibleであるか、 nothrow move constructibleであるか、referenceを持つのかによって、特殊化されるべきである.
      std::unique_ptrのデリーター指定コンストラクタの文面と実装を借用した 1
    • この2つ(訳注:scope_exitunique_resource)は<scope>という一つのヘッダーファイルにまとめる
    • scope_exitには、関数群を Alexandrescu氏 2 提唱の3 scope exit cases 3 に対し特殊化する。
      unique_resourceにはこれに類似のものは意図的に提供しない.
  • 動機とexample codeは長いから消した
  • UrbanaとCologneの会議からのフィードバックを受け修正

1.10.N3949からの変更点

  • 以下のとおり改名した(表は訳者作成)
N3949(変更前) N4189(変更後)
scope_guard_t scope_exit
scope_guard make_scope_exit
scope_guard_t::deleter scope_exit::exit_function
unique_resource_t unique_resource
unique_resource make_unique_resource
unique_resource_checked make_unique_resource_checked

理由 : ユーザーに対し、「ローカル変数に結果を保存する」というイメージではなく、「一時的で直ちに破壊される」というイメージを持ってもらうため 4

  • (訳注:改名後で言うmake系)関数群を(訳注:std::moveではなく)std::forwardをつかうようにし、reference型に推論(訳注:Template Argument Deductionのこと)されないようにした
  • 呼び出し引数が無い版のreset()関数の追加と、(訳注:引数無し、あり双方の)reset()関数のnoexcept指定がexit_functionに依存するようにした

1.11.N3830からの変更点

  • 以下のとおり改名した(表は訳者作成)
N3830 N3949
scoped_resource unique_resource_t
make_scoped_resource unique_resource
make_scoped_resource_checked unique_resource_checked
  • scope_guard_tクラスとscope_guardクラス生成関数によるscope guard機能の追加
  • シンプルなインターフェースにするため、(訳注:resourceを受け取る)可変長引数を削除した(訳注:単一引数に)。
    理由:lmanbdaは複数の引数を必要とする複雑な公開API群を扱える
  • lambda関数を指定しやすくするため、make系関数の引数で、デリーターを後に変更した

1.12.N3677からの変更点

  • 提案されていた4つのクラス(訳注:scoped_function, scoped_resource_unchecked, scoped_resource, unique_resource)を一つのクラス(訳注:scoped_resource)に置き換えた。可変長templateを使用してすべての使用ケースをカバーする。これは2013年秋のLEWG meetingで決定された。

The conscious decision was made to name the factory functions without "make", because they actually do not allocate any resources, like std::make_unique or std::make_shared do

  • (訳注:以下、N3830には載っていなくて、N3949の文章から追加された文言です。なのでN3949でmakeという名前が消された改名の説明だと思います)std::make_uniquestd::make_sharedがいい例ですが、クラス生成関数(訳注:N3830でいうmake_scoped_resource関数)は実際には一切リソースを確保しません。よって名前にmake_を含めないようにしました。

2 Introduction

STLには、std::unique_ptrstd::shared_ptrのようなポインタ型向けのRAIIクラスがある。この提案では、2つのRAIIクラスを提案する。これは、(オブジェクトが破棄されるのと同じように)スコープを抜けるときに実行されることを保証し、スコープに紐付けられてリソースを掃除/完成させるルーチンのクラスである。ただし、(訳注:スコープを抜けるより)早く開放された場合や、一つのリソースがあるときにすでに実行されたかムーブされた場合は実行されない。5

3 Acknowledgements(謝辞)

  • この提案は随分前にAndrej Alexandrescuがscope guardとして紹介し、C++Now 2012でも説明していたものを組み込んでいます。
  • This proposal would not have been possible without the impressive work of Peter Sommerlad who produced the sample implementation during the Fall 2013 committee meetings in Chicago. Peter took what Andrew Sandoval produced for N3677 and demonstrated the possibility of using C++14 features to make a single, general purpose RAII wrapper capable of fulfilling all of the needs presented by the original 4 classes (from N3677) with none of the compromises.
  • Gratitude is also owed to members of the LEWG participating in the February 2014 (Issaquah) and Fall 2013 (Chicago) meeting for their support, encouragement, and suggestions that have led to this proposal.
  • Special thanks and recognition goes to OpenSpan, Inc. (http://www.openspan.com) for supporting the production of this proposal, and for sponsoring Andrew L. Sandoval's first proposal (N3677) and the trip to Chicago for the Fall 2013 LEWG meeting. Note: this version abandons the over-generic version from N3830 and comes back to two classes with one or no resource to be managed.
  • Thanks also to members of the mailing lists who gave feedback. Especially Zhihao Yuan, and Ville Voutilainen.
  • Special thanks to Daniel Krügler for his deliberate review of the draft version of this paper (D3949).

脚注


  1. https://ask.fm/EzoeRyou/answer/133453870679 

  2. N3677の本文中および脚注のリンクを見る限り、この提案はAndrei Alexandrescu 氏の発表に影響を受けている模様 

  3. よくわからない。知ってる人教えてください 

  4. 江添さんのブログには「自動ストレージ上に確保する習慣をつけさせるため」と書いてある。訳に自信がない 

  5. returned by moving its valueってこの訳でいいのか・・・? 

11
9
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
9