Herb Sutter さんによる GotW C++14 版 勝手訳

Herb Sutter さんがブログ Sutter's Mill で GotW (Guru of the Week) の更新を始められましたね。
とても面白い連載なので、一気読みしてしまった挙句、ついカッとなって勢い余って日本語に訳してみました。

まずは「これから更新始めるよ~」という感じの前振りの記事から。
原文にキャッチアップするように GotW の翻訳を順次アップして行きたいと思います。

原文では、問題提起の記事を掲載してしばらくたってから解答の記事を載せていますが、私の翻訳はそんな周りくどいことはせずに解答の記事を直接翻訳して載せようと思います。
また、翻訳にあたっては、原文の英語もそのまま載せて、日本語訳は引用の形式を使って、パラグラフなどの単位で対訳のように載せていきます。

原文著者(Herb Sutter 氏)の許可は得ていませんし、私の英訳がヒドいクオリティである(用語の統一がとれていないとか、誤訳が含まれているとか)かもしれませんのでそこのところはご理解いただければと思います。



原文:http://herbsutter.com/2013/05/04/guru-of-the-week-and-the-exceptional-c-series/

Guru of the Week and the Exceptional C++ Series

今週の Guru(Guru of the Week)と Exceptional C++ シリーズ

2013-05-04 by Herb Sutter

It’s time for me to pick up Guru of the Week (GotW) again in earnest, as part of work on revising my three Exceptional C++ books for today’s C++. Most Exceptional C++ Items are enhanced versions of GotW issues, after all, so the simplest and best place to start is with GotW. It’s also much easier to write (and read) one short piece at a time.

私の三冊の Exceptional C++ 本を今日の C++ 向けに改訂する仕事の一部として、Guru of the Week (GotW) を再び真面目に取り上げる時が来ました。Exceptional C++ のほとんどの項目は GotW の改良版であり、結局のところ GotW から始めるのが最もシンプルでありベストです。一度に少しずつ書くほうが楽ということもありますし、きっと読む方のみなさんもその方が楽なことでしょう。

Last spring I wrote a few GotWs, but life got busy with the Standard C++ Foundation, isocpp.org, and C++14. I was also trying to figure out what reasonable tools to use to write new Items, since I wanted to publish the same Item on this blog and then in e-book and dead tree editions, all with a single source while maintaining a pleasing format. I’ve found a provisional promising tool (Word!) and format for that I’ll try out for now, and you’ll see it in the next GotW issue to be posted soon.

昨年の春、私はいくつかの GotW を書きましたが、Standard C++ Foundation や isocpp.org、C++14 で忙しくなってしまいました。また、私は新しい GotW の項目を書くのに適したツールはないかと探していました。項目を一度書いたら、このブログに載せるだけでなく同じものを電子書籍と紙の書籍として出版したいと思っていて、しかもメンテが楽なフォーマットが望ましいのですが、暫定でたどり着いたのはとても有望なツール(Word!)です。フォーマットは今試してみているところです。もうすぐ次の GotW の投稿をお目にかけることができるでしょう。

But a major issue was figuring out the right balance between updating existing GotW issues and writing new ones.

しかし大きな問題は、既存のGotWを更新するのと新しいものを書くのとのちょうどいいバランスを見つけることです。

Here’s where I landed…

私がたどり着いたのは…

First, I’ve decided to keep and revise nearly all of my existing Items.

まず、ほとんどすべての既存の項目を残し、更新することに決めました。

This is not an obvious choice: As a counterexample, some authors have decided to not revise their existing books, but to write new books about C++11, for various reasons including that they expect the advice in the earlier books is still current. I think the latter is partly true, because of course the topics are still current and the code still compiles – C++11 has a great backward compatibility story with C++98.

これは明白な選択ではないかもしれません。反例として、既存の本を書きなおすのではなく、既存の本の内容がいまだに古くなっていないなどの理由で C++11 についての新しい本を書き直すと決めた著者がいるとしましょう。既存の本の内容がまだ古くなっていないというのは部分的に真実です。取り上げているトピックは当然古くなっていませんし、コードも未だにコンパイルできます。C++11 は C++98 に対して素晴らしい後方互換性を持っています。

However, when I looked through my own articles and Items, every single one I looked at had two qualities:

しかしながら、私の記事と GotW の項目をひと通り見なおしてみたところ、それらは 2 つのクオリティを持っているように見えました。

  • The topic was still current and useful, so it should still be covered. (With very rare exceptions like material discussing the now-removed export template feature.)

トピックはまだ古くなっておらず有用なので、記事や項目の一つとしてカバーされてしかるべきもの。(とてもレアな例外として、現時点で削除されている export テンプレート機能に関する議論のようなものもあります)

  • The code and discussion were addressed with C++98-era advice that is now dated and/or incomplete.

コードや議論が C++98 時代のもので、古くなっていたり不完全になっているもの

For example, nearly every GotW’s code should just naturally use auto and { } initialization. Most should use or actively discuss move semantics, lambdas, and other C++11 features. And it’s hard to write about existing features like virtual function overriding (as in GotW #5) without feeling something important is missing unless there’s also discussion of the way C++ has extended the existing feature, such as with override and final.

たとえば、ほとんどすべての GotW のコードは auto と {} による初期化を使うべきですし、ほとんどのものはムーブセマンティクスやラムダなどの C++11 の機能を使うか少なくとも積極的に検討すべきです。そして 仮想関数のオーバーライドのような機能(GotW#5で取り上げられています)は、override や final といった C++の既存機能に対する拡張について触れなければ、何か大事なものを忘れているという違和感なしに書くことは難しいでしょう。

Second, I’ve decided not to target C++11 – rather, I’m going to write for C++14. Why would I do that, when C++ compilers [preemptive snarky remark: including my own team’s] are still catching up to C++11? For several good reasons:

二つめに、C++11 を対象にせず、C++14 を対象にすることに決めました。なぜそうするか。C++ コンパイラは [プリエンプティブでウザい備考:私のチームのも含め] 、いまだに C++11 に追いつこうとしてる段階なのに?いくつかの理由があります:

  • C++14 is going to be current C++ soon. As of April, it’s now feature-complete, and the detailed technical specification should be frozen this fall or winter (leaving time for ISO ballots and “’14” publication). Compilers and libraries are already aggressively implementing it. I wouldn’t be surprised if we had multiple fully-conforming C++14 implementations by the end of next year.

C++14 は間も無く、"カレント" の C++ になります。4 月の時点で、機能的には全て決定していて、詳細な技術仕様がこの秋か冬には確定します(ISO の投票と “’14” の発行に向け、出発です)。コンパイラとライブラリはすでに意欲的に C++14 を実装しています。来年の末には C++14 完全準拠の実装が複数あっても驚くに値しないでしょう。

  • C++14 is a small delta, a minor release aimed mainly at “completing C++11.” And it does complete C++11 well, to the point where as an author I really want to teach the complete story. I want to teach the complete “avoid writing new and delete” guidance that C++14 make_unique fully enables so that I can stop mumbling “except when you create a unique_ptr” as a special case. I want to write generic lambdas with C++14’s terse auto parameters that makes lambdas shorter to write and more flexible, and stop repeating parameter type names. I want to use C++14 move-capture in lambdas that makes them complete, and not feel obliged to talk about artificially putting objects on the heap and capturing shared_ptrs to them by value as a workaround for lack of move capture. And, in a few places where appropriate, I want to be free to use a few smaller C++14 tidbits like optional that can affect interface design guidance now that they’re in the draft standard.

C++14 は小さな差分であり、主に C++11 を完全なものにすることを意図したマイナーリリースです。そしてC++14 は本当に C++11 を完全なものにしますし、私も著者という立場から完全なものを伝えたいと思います。私は "new と deleteを書くことを避ける" ガイダンスを完全な形でお伝えしたいと思っていますが、それは C++14 の make_unique によって可能となります。make_unique のおかげで、「特殊なケースとして "unique_ptrを作る時は除外"」といってモゴモゴするのをやめることができるのです。私はジェネリックなラムダを、C++14 の auto を使って書きたいです。それによってラムダを短く書くこともできますし、より柔軟になり、パラメータの型名を何度も書くのをやめることができます。私はラムダにおいて C++14 のムーブキャプチャを使いたいです。それによりラムダは完全なものになりますし、ムーブキャプチャが無いことに対するワークアラウンドとしてオブジェクトをヒープに突っ込んでそれへのポインタを shared_ptr でキャプチャすることについて語らなくてはならないという気持ちからも解放されるわけです。そして、optional のような、小さいけれどインターフェイスデザインのガイダンスに影響を及ぼすようなすでに C++14 のドラフト入りしているいくつかのものを、それにふさわしいいくつかの場所では自由に使いたいです。

In addition, I’ve found that C++11/14 is changing my coding style, including my basic variable declaration style. I now think auto should be used consistently to declare local variables, even when you want to commit to a specific type(!) – this certainly surprised me, and likely will surprise you too, so I’ll cover that this year in a GotW and a recorded talk, no later than at C++ and Beyond later this year and possibly sooner.

くわえて、C++11/14は私のコーディングスタイルを変えたことに気づきました。その中には基本的な変数の宣言のスタイルも含まれます。私は今ではローカル変数を宣言するところでは一貫してautoを使うべきだと考えるようになりました。特定の型に確定したいときであってもです!これは特に私自身を驚かせました。あなたもおそらく驚くことになるでしょう。それで私はこの話題について今年中に GotW とトークの録音でカバーしようと思います。今年後半の C++ and Beyond より遅くはならないと思います。可能ならすぐにでもと思っています。

I’ve converged on the above by updating the first dozen Exceptional C++ Items (in GotW order), trying out alternatives and seeing how well each one worked out. Those first dozen Items are now done and I’m pleased with the result, showing off auto and move and generic lambdas and mutexes/atomics and the new meanings of const and mutable and everything else, most of their code tested against various of the latest compilers, their advice updated to be current with current C++. And in every single GotW, no matter how short, it just felt right to:

Exceptional C++ の最初の1ダースの項目を(GotW の順で)更新して、他のアイディアも試してそれがうまくいくかどうかを見ながら、上記の結論に収束しました。その最初の1ダースの項目は完了して、私はその結果に満足しています。auto やムーブ、ジェネリック ラムダ、ミューテックスとアトミック、const や mutable の新しい意味などをご覧に入れます。ほとんどのコードは複数の最新のコンパイラでテストしました。アドバイスは最新の C++ に合わせて最新になるように更新されました。すべての GotW ひとつひとつにおいては、それがどんなに短くても、以下のことが正しいと感じました:

  • keep the topic, because each topic is still as relevant and useful as ever;

トピックを維持する。なぜならすべてのトピックは未だ現役でありこれまでどおり有用;

  • revise the C++98 code and advice, because in every case both the code examples and the discussion was dated and/or incomplete, usually both; and

C++98 のコードとアドバイスは書きなおされる。なぜならすべてのケースにおいて、コード例と議論が古くなっているか不完全となっている(たいていは両方);そして

  • skip C++11 to write directly for the complete story in C++14 without apology, because it let me avoid digressions for workarounds not needed in C++14, which made the text cleaner and simpler.

特に断りなく C++11 をスキップして C++14 で書く。なぜなら C++14 を使うことで本題から外れたワークアラウンドを避けることができ、本文がより明確でよりシンプルになる。

As I’ve revised Items, I’ve found that some updates are small but pervasive, such as using auto. Others are extensive, with entire subsections completely rewritten, thrown out as no longer relevant, or newly added to cover essential new sides of the story in modern C++. For example, GotW #6 about const-correctness is now a two-parter, covering the new meanings of const and mutable. Watch for it soon.

GotW の項目を書き直してみて、いくつかの変更は、例えば auto を使うことのように、小さいけれども広範囲にわたっていることに気が付きました。他にも、大規模なもの、サブセクション全体を完全に書きなおすようなもの、もはや今日的な意味を持たないので捨て去られるもの、モダンな C++ において必須の新たな側面をカバーするために追加されたものがあります。たとえば、const 正当性について書かれた GotW #6 は const と mutable の新しい意味をカバーするために 2 部構成になりました。乞うご期待。

A word about numbering and sequence: My plan is to update and post most GotWs in numerical order starting with GotW #1, but every so often we’ll jump forward to new high numbers (starting with #89) as I create ones that weren’t originally a GotW – a Sutter’s Mill magazine article that wasn’t originally published as a GotW, or a brand-new C++14 topic. So for example you might see a sequence like “… 23, 24, 91, 25, 26, …”. All GotWs will have substantial new material, but the newly minted high numbers will be entirely on brand-new topics that didn’t arise in C++98 or just weren’t covered in GotW form before. (The handful of GotWs I wrote last year will be renumbered as they’re folded into the original series, either as updates or with new numbers in the original sequence if they’re on completely new topics. )

番号の付け方と順番について:私のプランは、#1 から始めて番号順にほとんどの GotW を更新していこうとしています。でもたまに既存の GotW にはなかったような新しい大きい番号(#89 から始まります)にジャンプして、GotW としては元々発行されていなかったような Sutter's Mill マガジンの記事や完全に新しい C++14 のトピックを載せます。なので、読者はたとえば "… 23, 24, 91, 25, 26, …" のような順番で記事を見ることになるでしょう。すべての GotW は充実した新しい内容となりますが、その新しく付け加えられた大きな番号の記事は C++98 では書かれなかったか GotW の形式では過去にカバーされなかったような全く新しいトピックになります。(昨年書いた一握りの GotW は、オリジナルのシリーズの中に違う番号で組み込まれるか、もしそれが完全に新しいトピックなら新しい番号で追加されます。)

I’ll start posting fresh C++11/14 GotWs in the next few days. As usual, I’ll first post a problem by itself to invite discussion about possible solutions, then after there’s been time for comments I’ll post my own solution and the next problem.

近日中に、新鮮な C++11/14 の GotW の投稿を開始します。まず問題を投稿して、それについての議論や解答案を喚起します。そのあとでしばらくコメントのための時間をおいてから私の解答を投稿し、次の問題に移ろうと思っています。


It’s kind of exciting to write “GotW14” – today’s C++ really does feel like a fresh new language.

"GotW14" を書くのはエキサイティングです。今日の C++ は本当に新鮮な新しい言語のような気がします。

I hope you enjoy them as much as I enjoy writing them.

私が書くのを楽しむのと同じくらい、みなさんも楽しんでいただけたらと思います。

Exceptional C++―47のクイズ形式によるプログラム問題と解法 (C++ in‐Depth Series)

Exceptional C++―47のクイズ形式によるプログラム問題と解法 (C++ in‐Depth Series)

  • 作者: ハーブサッター,浜田光之,Harb Sutter,浜田真理
  • 出版社/メーカー: ピアソンエデュケーション
  • 発売日: 2000/11
  • メディア: 単行本
  • 購入: 9人 クリック: 134回
  • この商品を含むブログ (62件) を見る

Exceptional C++ Style―40のクイズ形式によるプログラム問題と解法=スタイル編 (C++ in‐Depth Series)

Exceptional C++ Style―40のクイズ形式によるプログラム問題と解法=スタイル編 (C++ in‐Depth Series)

  • 作者: ハーブサッター,浜田光之,Herb Sutter,浜田真理
  • 出版社/メーカー: ピアソンエデュケーション
  • 発売日: 2006/09
  • メディア: 単行本
  • 購入: 3人 クリック: 32回
  • この商品を含むブログ (29件) を見る

More Exceptional C++ さらに40のクイズ形式によるプログラム問題と解法 (C++ In‐Depth Series)

More Exceptional C++ さらに40のクイズ形式によるプログラム問題と解法 (C++ In‐Depth Series)

  • 作者: ハーブサッター,Herb Sutter,浜田光之,浜田真理
  • 出版社/メーカー: ピアソンエデュケーション
  • 発売日: 2008/11/05
  • メディア: 単行本(ソフトカバー)
  • 購入: 2人 クリック: 32回
  • この商品を含むブログ (19件) を見る

Effective C++ 原著第3版 (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES)

Effective C++ 原著第3版 (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES)


Efficient C++パフォーマンスプログラミングテクニック

Efficient C++パフォーマンスプログラミングテクニック

  • 作者: ダブブルカ,デビットメイヒュ,浜田光之,Dov Bulka,David Mayhew,浜田真理
  • 出版社/メーカー: ピアソンエデュケーション
  • 発売日: 2000/07
  • メディア: 単行本
  • 購入: 9人 クリック: 149回
  • この商品を含むブログ (31件) を見る