Template Programming Language

//ControlStructure.h
#pragma once
template<bool Cond, class Then, class Else>
struct make_if {
    using result = typename Then;
};
template<class Then, class Else>
struct make_if<false, Then, Else> {
    using result = typename Else;
};
template<bool Cond, class Then, class Else>
using _If = typename make_if<Cond, Then, Else>::result;         //延遲求值
template<bool Cond, class Then, class Else>
using If = typename _If<Cond, Then, Else>::result; //立即求值

//DataType.h
#pragma once
template<int N>
struct Int { static int const result = N; };
//list.h
//下面開頭帶_為延遲求值, 否則是立即求值
#pragma once
#include "ControlStruct.h"
struct Null { using result = Null; };

//-------------定義序?qū)?-------------
template<class left, class right>
struct Cons {
    using result = Cons<left, right>;
    using car = left;
    using cdr = right;
    enum { length = cdr::length + 1 };
};
template<class left>
struct Cons<left, Null> {
    using result = Cons<left, Null>;
    using car = left;
    using cdr = Null;
    enum { length = 1 };
};

//-----------------------------------

//-------------定義鏈表--------------
template<class head, class ...tail>
struct make_list {
    using result = Cons<head, typename make_list<tail...>::result>;
};
template<class head>
struct make_list<head> {
    using result = Cons<head, Null>;
};
template<class ...Arg>
using List = typename make_list<Arg...>::result;

//-----------------------------------

template<class cons>
struct _Car {
    using result = typename cons::car;
};
template<class cons>
using Car = typename _Car<cons>::result;

template<class cons>
struct _Cdr {
    using result = typename cons::cdr;
};
template<class cons>
using Cdr = typename _Cdr<cons>::result;

template<class cons>
struct _Length {
    enum{ result = cons::length };
};
template<class cons>
using Length = typename _Length<cons>::result;

//-------------鏈表查找--------------
template<class list, int n>
struct _Find {
    using result = typename _Find<typename list::cdr, n - 1>::result;
};
template<class head>
struct _Find<make_list<head>, 0> {
    using result = typename make_list<head>::car;
};
template<class list>
struct _Find<list, 0> {
    using result = typename list::car;
};
template<class list, int n>
using Find = typename _Find<list, n>::result;
//-----------------------------------

//-------------鏈表拷貝--------------
template<class list, int start , int end>
struct _Copy {
    using result = Cons<Find<list, start>,
               typename _Copy<typename list::cdr, start, end - 1>::result>;
};
template<class list, int start>
struct _Copy<list, start, start> {
    using result = Cons<Find<list, start>, Null>;
};
template<class list, int start, int end>
using Copy = typename _Copy<list, start, end>::result;
//-----------------------------------

//-------------鏈表合并--------------

template<class T, class ...Arg>
struct _Append {
    template<class cons1, class cons2>
    struct make_append {
        using result = Cons<typename cons1::car, 
                            typename make_append<typename cons1::cdr, cons2>::result>;
    };
    template<class cons2>
    struct make_append<Null, cons2> {
        using result = Cons<typename cons2::car, 
                            typename make_append<Null, typename cons2::cdr>::result>;
    };
    template<>
    struct make_append<Null, Null> {
        using result = Null;
    };

    using result = typename make_append<T, typename _Append<Arg...>::result>::result;
};
template<class list>
struct _Append<list> {
    using result = list;
};
template<class T, class ...Arg>
using Append = typename _Append<T, Arg...>::result;
//-----------------------------------



//-------------鏈表修改--------------
template<class list, int index, class val>
struct _Change {
    using result = Append<Copy<list, 0, index - 1>,
                          Cons<val,
                               If<(index == list::length - 1),
                                   Null,
                                  _Copy<list, index + 1, list::length - 1>>>>;//延遲求值
};
template<class list, class val>
struct _Change<list, 0, val> {
    using result = Append<Null,
                          Cons<val, 
                               Copy<list, 1, list::length - 1>>>;
};
template<class list, int index, class val>
using Change = typename _Change<list, index, val>::result;
//-----------------------------------*

template<class list, int a, int b>
struct _Swap {
    using result = Change<Change<list,a,Find<list, b>>,b, Find<list, a>>;
};
template<class list, int a, int b>
using Swap = typename _Swap<list, a, b>::result;
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市断医,隨后出現(xiàn)的幾起案子偶摔,更是在濱河造成了極大的恐慌骚亿,老刑警劉巖鸟整,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件怎披,死亡現(xiàn)場離奇詭異翘盖,居然都是意外死亡桂塞,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門馍驯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來阁危,“玉大人,你說我怎么就攤上這事汰瘫】翊颍” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵吟吝,是天一觀的道長菱父。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么浙宜? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任官辽,我火速辦了婚禮,結果婚禮上粟瞬,老公的妹妹穿的比我還像新娘同仆。我一直安慰自己,他們只是感情好裙品,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布俗批。 她就那樣靜靜地躺著,像睡著了一般市怎。 火紅的嫁衣襯著肌膚如雪岁忘。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天区匠,我揣著相機與錄音干像,去河邊找鬼。 笑死驰弄,一個胖子當著我的面吹牛麻汰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播戚篙,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼五鲫,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了岔擂?” 一聲冷哼從身側響起位喂,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎智亮,沒想到半個月后忆某,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體点待,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡阔蛉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了癞埠。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片状原。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖苗踪,靈堂內(nèi)的尸體忽然破棺而出颠区,到底是詐尸還是另有隱情,我是刑警寧澤通铲,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布毕莱,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏朋截。R本人自食惡果不足惜蛹稍,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望部服。 院中可真熱鬧唆姐,春花似錦、人聲如沸廓八。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽剧蹂。三九已至声功,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間宠叼,已是汗流浹背减噪。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留车吹,地道東北人筹裕。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像窄驹,于是被迫代替她去往敵國和親朝卒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內(nèi)容