コンパイルタイムにリスト構造作ってルックアップ
コンパイル時に
typedef linked_list<int_pair<1, 'a'>, linked_list<int_pair<2, 'b'> > > tbl;
みたく変換テーブル (1 => a, 2 => b) を構築してルックアップするってのを何となく書いてみた。リンクリストだけど定数のルックアップならコンパイル時に解決できるから O(0) だぉ。って使えなさそーorz
#include <iostream> using namespace std; template<typename Value, typename Next = void> struct linked_list { typedef Value value; typedef Next next; }; template<int First, int Second> struct int_pair { const static int first = First; const static int second = Second; }; template<typename List> struct lookup { static int f(int t) { if (t == List::value::first) return List::value::second; return lookup<typename List::next>::f(t); } }; template<> struct lookup<void> { static int f(int) { return 0; } }; typedef linked_list<int_pair<1, 'a'>, linked_list<int_pair<2, 'b'> > > tbl; int main(void) { cout << "lookup(1)=" << (char)lookup<tbl>::f(1) << endl; cout << "lookup(2)=" << (char)lookup<tbl>::f(2) << endl; return 0; }