SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

DeveloperZine(デベロッパージン)- エンジニアの意思決定を支える技術情報メディア ProductZine

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

Bjarne Stroustrup氏との対話

自然な思考で当たり前のプログラミングを~Bjarne Stroustrup氏との対話~

第7回

Question 7: どちらのコードを使うべき?

I found two one-line sample codes in the paper(PDF). They are quite short but interest me a lot. The codes are as follows;

 

  auto q = find(vi, 7);

  auto q = find(vi.begin(), vi.end(), 7);

 

Are those codes are interchangeable in C++0x? If so, which code would you recommend in the classroom? To use the first code, I think we have to learn a lot about C++ history and new language extensions such as type deduction and interface contract. The second code looks wordy but is easier to understand. At least to me. To find something, we first define its search range. Am I wrong? To meet Dr. Stroustrup, we search U.S for you first, not Japan. Any comment would be appreciated here. Do you think the first code makes compiler writers happy, not C++ learners?

 あなたの論文『Evolving a language in and for the real world: C++ 1991-2006』(PDF)には次のような2つの1行サンプルコードがあります。

auto q = find(vi, 7);
auto q = find(vi.begin(), vi.end(), 7);

 これら2つのコードは、C++0x仕様では交換可能ですか? もしそうなら、あなたはC++初学者にどちらを勧めますか? 最初のコードを使用するためには、C++の歴史、型類推、インターフェース契約などの新しい言語拡張を学んでおく必要があると思います。第2コードは冗長そうに見えますが、私にはより分かりやすいように感じます。何か大切なものを見つけたい場合、私たちはまずその探索範囲を定義します。そうではありませんか。Stroustrup博士に面会したい場合、日本ではなく、まず米国を探すはずです。コメントを頂戴できると幸いです。第1サンプルコードはコンパイラーライタを嬉しがらせることにはなっても、C++学習者はどうでしょうか。

Stroustrup氏の回答1: コンテナーの標準アルゴリズムは標準仕様では提供されていない

Actually, the container forms of the standard algorithms are not provided in the standard, so if you want them, you have to define them yourself.

For example:

 

  template<Container C, class V>C::const_iterator find(const C& c, const V& v)

  requires HasEqualTo<C::value_type,V>

  {

    return find(c.begin(),c.end());

  }

 

This is basically a formal notation of what you'll have to understand to use find() on a vector. Fortunately, it is easier to use an operation than to specify/define/implement it.

 実際のところ、標準仕様は標準アルゴリズムというものをコンテナーに提供していません。必要な場合には、例えば、次のようなアルゴリズムを自分で定義しなければなりません。

template<Container C, class V>C::const_iterator find(const C& c, const V& v)
requires HasEqualTo<C::value_type,V>
{
    return find(c.begin(),c.end());
}

 ベクター上でfind()を使用するためには、この表記法を理解しておかなければなりません。定義から実装まで記述するわけではなく、操作を使用するだけですから、開発者側の負担は少ないはずです。

次のページ
Stroustrup氏の回答2: 私は第1のコードを好みます

この記事は参考になりましたか?

Bjarne Stroustrup氏との対話連載記事一覧

もっと読む

この記事の著者

豊田 孝(トヨタ タカシ)

「Windows PowerShell実践スクリプティング―オブジェクト指向と集合指向の統合シェル」(秀和システム発行)と「IT技術者として生き抜くための十ヶ条」(翔泳社発行)の近著2冊にて本音の数%を吐露。最近の活動傾向は、こちらを参照してください。 

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

CodeZine(コードジン)
https://codezine.jp/article/detail/3370 2008/12/24 14:00

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー