東方算程譚

Oriental Code Talk ── επιστημηが与太をこく、弾幕とは無縁のシロモノ。

目次

Blog 利用状況

ニュース

著作とお薦めの品々は

著作とお薦めの品々は
東方熱帯林へ。

あわせて読みたい

わんくま

  1. 東京勉強会#2
    C++/CLI カクテル・レシピ
  2. 東京勉強会#3
    template vs. generics
  3. 大阪勉強会#6
    C++むかしばなし
  4. 東京勉強会#7
    C++むかしばなし
  5. 東京勉強会#8
    STL/CLRによるGeneric Programming
  6. TechEd 2007 @YOKOHAMA
    C++・C++/CLI・C# 適材適所
  7. 東京勉強会#14
    Making of BOF
  8. 東京勉強会#15
    状態遷移
  9. 名古屋勉強会#2
    WinUnit - お気楽お手軽UnitTest

CodeZine

  1. Cで実現する「ぷちオブジェクト指向」
  2. CUnitによるテスト駆動開発
  3. SQLiteで組み込みDB体験(2007年版)
  4. C++/CLIによるCライブラリの.NET化
  5. C# 1.1からC# 3.0まで~言語仕様の進化
  6. BoostでC++0xのライブラリ「TR1」を先取りしよう (1)
  7. BoostでC++0xのライブラリ「TR1」を先取りしよう (2)
  8. BoostでC++0xのライブラリ「TR1」を先取りしよう (3)
  9. BoostでC++0xのライブラリ「TR1」を先取りしよう (4)
  10. BoostでC++0xのライブラリ「TR1」を先取りしよう (5)
  11. C/C++に対応した、もうひとつのUnitTestFramework ─ WinUnit
  12. SQLiteで"おこづかいちょう"
  13. STL/CLRツアーガイド
  14. マージ・ソート : 巨大データのソート法
  15. ヒープソートのアルゴリズム
  16. C++0xの新機能「ラムダ式」を次期Visual Studioでいち早く試す
  17. .NETでマンデルブロ集合を描く
  18. .NETでマンデルブロ集合を描く(後日談)
  19. C++/CLI : とある文字列の相互変換(コンバージョン)
  20. インテルTBBによる選択ソートの高速化
  21. インテルTBB3.0 によるパイプライン処理
  22. Visual C++ 2010に追加されたSTLアルゴリズム
  23. Visual C++ 2010に追加されたSTLコンテナ「forward_list」
  24. shared_ptrによるObserverパターンの実装
  25. .NETでマンデルブロ集合を描く(番外編) ── OpenCLで超並列コンピューティング
  26. StateパターンでCSVを読む
  27. 状態遷移表からStateパターンを自動生成する
  28. 「ソートも、サーチも、あるんだよ」~標準C++ライブラリにみるアルゴリズムの面白さ
  29. インテルTBBの同期メカニズム
  30. なぜsetを使っちゃいけないの?
  31. WPFアプリケーションで腕試し ~C++でもWPFアプリを
  32. C++11 : スレッド・ライブラリひとめぐり
  33. Google製のC++ Unit Test Framework「Google Test」を使ってみる
  34. メールでデータベースを更新するココロミ
  35. Visitorパターンで遊んでみたよ
  36. Collection 2題:「WPFにバインドできる辞書」と「重複を許す検索set」
  37. Visual C++ 2012:stateless-lambdaとSQLiteのぷち拡張
  38. 「Visual C++ Compiler November 2012 CTP」で追加された6つの新機能

@IT

  1. Vista時代のVisual C++の流儀(前編)Vista到来。既存C/C++資産の.NET化を始めよう!
  2. Vista時代のVisual C++の流儀(中編)MFCから.NETへの実践的移行計画
  3. Vista時代のVisual C++の流儀(後編) STL/CLRによるDocument/Viewアーキテクチャ
  4. C++開発者のための単体テスト入門 第1回 C++開発者の皆さん。テスト、ちゃんとしていますか?
  5. C++開発者のための単体テスト入門 第2回 C++アプリケーションの効率的なテスト手法(CppUnit編)
  6. C++開発者のための単体テスト入門 第3回 C++アプリケーションの効率的なテスト手法(NUnit編)

AWARDS


Microsoft MVP
for Visual Developer - Visual C++


Wankuma MVP
for いぢわる C++


Nyantora MVP
for こくまろ中国茶

Xbox

Links

記事カテゴリ

書庫

日記カテゴリ

アヒルの判別

インスパイア元 → アヒル型

polymorphism:多態 てーのは実行時にオブジェクトの型を
判別してそれに応じてメソッドを呼び分けるんだけども、
templateを使ったcompile-time-polymorphism:コンパイル時多態ってワザがあるます。

静的ダックタイピングの裏返しってゆーかなんてゆーか
コンパイル時に型を判別し、それに応じて関数を呼び分けます。

#include <iostream>
#include <string>

// かわいい/みにくい識別子
struct pretty_category {};
struct ugly_category {};

// かわいいアヒルはコチラへ
template<typename Duckling>
void identify_(const Duckling& duckling, pretty_category) {
  std::cout << duckling.str() << " はかわいいコ。くわっくわっ\n";
}

// みにくいアヒルはコチラへ
template<typename Duckling>
void identify_(const Duckling& duckling, ugly_category) {
  std::cout << duckling.str() << " はやがて美しい白鳥に。\n";
}

// コンパイル時にかわいい/みにくいを判別して上のいずれかに振り分け
template<typename Duckling>
void identify(const Duckling& duckling) {
  identify_(duckling, Duckling::category());
}

// ふつーのアヒルの子
class NormalDuckling {
public:
  typedef pretty_category category;
  std::string str() const { return "きいろいアヒルの子"; }
};

// みにくいアヒルの子
class UglyDuckling {
public:
  typedef ugly_category category;
  std::string str() const { return "みにくいアヒルの子"; }
};

int main() {
  identify(NormalDuckling());
  identify(UglyDuckling());
}

----- 実行結果 -----
きいろいアヒルの子 はかわいいコ。くわっくわっ
みにくいアヒルの子 はやがて美しい白鳥に。

投稿日時 : 2007年8月31日 10:56

コメントを追加

# re: アヒルの判別 2007/08/31 11:37 アキラ

さすがC++だw

# re: アヒルの判別 2007/08/31 11:49 επιστημη

おもしろいよね。
ダックタイピングでアヒル化しておいて、
アヒルに付いたタグで振り分けてるですね。

# re: アヒルの判別 2007/08/31 12:19 ddnp

美しいです。正直しびれます。
応用は・・・ハック的にならざるを得ないかな
無い頭絞って考えます

# re: アヒルの判別 2007/08/31 13:40 επιστημη

応用の一例:
struct sorted_tag {};
struct nosorted_tag {};

template<typename Container, typename T>
bool find_(const Container& c, T target, sorted_tag)
{ バイナリサーチする }

template<typename Container, typename T>
bool find_(const Container& c, T target, nosorted_tag)
{ リニアリサーチする }

template<typename Container, typename T>
bool find(const Container& c, T target)
{ return find_(c,t,Container::category()); }

とかね。
ソートされたContainerで
 typedef sorted_tag category;
しておけば自動的にバイナリサーチします。

# re: アヒルの判別 2007/08/31 14:51 uskz

有名な所ではSTLのiterator conceptのrefinement階層に応じたalgorithmの実装の切り替えなどに使われてますね.

template <typename T>
struct category_of
{ typedef typename T::category type; };

のようなtraits classを用いて,

template<typename Duckling>
void identify(const Duckling& duckling) {
identify_(duckling, typename category_of<Duckling>::type());
}

のように呼び出しておけば,category_ofを特殊化することによってcategoryをtypedefしていない型にも適用できるようになるのでさらに汎用的.

# re: アヒルの判別 2007/08/31 15:09 επιστημη

そそそ。iterator_traits に使われてますな。
んでもって iterator_traits<T*> を特殊化してある、と。

# Hmm is anyone else experiencing problems with the pictures on this blog loading? I'm trying to find out if its a problem on my end or if it's the blog. Any suggestions would be greatly appreciated. 2019/05/05 0:10 Hmm is anyone else experiencing problems with the

Hmm is anyone else experiencing problems with the pictures on this blog loading?
I'm trying to find out if its a problem on my end or if it's the blog.

Any suggestions would be greatly appreciated.

# Remarkable! Its truly awesome article, I have got much clear idea regarding from this piece of writing. 2019/06/03 12:14 Remarkable! Its truly awesome article, I have got

Remarkable! Its truly awesome article, I have
got much clear idea regarding from this piece of writing.

# Incredible quest there. What happened after? Take care! 2019/06/15 9:29 Incredible quest there. What happened after? Take

Incredible quest there. What happened after? Take care!

# Yes! Finally something about plenty of fish dating site. 2019/07/15 16:36 Yes! Finally something about plenty of fish dating

Yes! Finally something about plenty of fish dating site.

# Yes! Finally something about plenty of fish dating site. 2019/07/15 16:37 Yes! Finally something about plenty of fish dating

Yes! Finally something about plenty of fish dating site.

# Yes! Finally something about plenty of fish dating site. 2019/07/15 16:38 Yes! Finally something about plenty of fish dating

Yes! Finally something about plenty of fish dating site.

# Yes! Finally something about plenty of fish dating site. 2019/07/15 16:39 Yes! Finally something about plenty of fish dating

Yes! Finally something about plenty of fish dating site.

# Can you tell us more about this? I'd want to find out some additional information. 2019/08/24 14:07 Can you tell us more about this? I'd want to find

Can you tell us more about this? I'd want to find out some additional information.

# Hello! I understand this is kind of off-topic but I needed to ask. Does operating a well-established blog like yours take a massive amount work? I'm brand new to operating a blog but I do write in my journal everyday. I'd like to start a blog so I will 2019/09/08 23:27 Hello! I understand this is kind of off-topic but

Hello! I understand this is kind of off-topic but I needed to ask.
Does operating a well-established blog like yours take a massive amount work?
I'm brand new to operating a blog but I do write in my
journal everyday. I'd like to start a blog so I will be able to share my own experience and feelings online.
Please let me know if you have any kind of recommendations or tips
for brand new aspiring blog owners. Thankyou!

# TKgOGlIZQkdoSqSBv 2021/07/03 2:54 https://amzn.to/365xyVY

It as hard to find experienced people on this topic, however, you sound like you know what you are talking about! Thanks

# Illikebuisse cxwnf 2021/07/04 8:20 pharmaceptica

side effects of chloroquine https://www.pharmaceptica.com/

# erectile coffee 2021/07/10 23:49 dolquine

hydrochloroquin https://plaquenilx.com/# hcqs tablet

# re: ?????? 2021/07/12 22:00 hydroxychloroquine meaning

chloroquine tablets amazon https://chloroquineorigin.com/# hydroxychloroquine tablet

# re: ?????? 2021/07/23 21:33 hydrocloroquine

how does chloroquine work https://chloroquineorigin.com/# hydroxychloroquine risks

# vpxesqbtgiyb 2022/05/08 16:18 pxdwag

side effects of hydroxychlor 200 mg https://keys-chloroquinehydro.com/

# yrrxvwdivdwt 2022/05/10 9:17 zvykjm

hydroxychloroquine 200 mg https://keys-chloroquineclinique.com/

タイトル
名前
URL
コメント