第二章 技術

2021-07-03 07:04:57 字數 4695 閱讀 2875

1、編譯器assert

#pragma warning(disable:4101)

/////使用陣列,無法傳入額外引數

#define static_assert1(express) // 使用大括號是為了保證作用域,防止名字衝突

////使用類建構函式,同樣無法傳入額外引數方便上層呼叫者知道是在**

template

class ctestassert2;

template<>

class ctestassert2{};

#define static_assert2(express)

//// 使用類構造函式引數,可以讓上層傳入字串

template

class ctestassert3;

// 為true時任意型別都能轉換

template<>

class ctestassert3

;// 為false時,使用編譯器自動生成的預設建構函式,甚至沒有建構函式

template<>

class ctestassert3

;#define static_assert3(express,usermsg) \

;\error##usermsg tmp;\

sizeof(ctestassert3(tmp));\

}void ctestthchniques::testcompileassert()

2、常整數對映為型別,方便實現函式的過載

// 將整數對映成為乙個型別

template

struct int2type;};

//class cisclone

cisclone(){};

private:

cisclone(const cisclone& rhs);

};class cnotclone

cnotclone(){}

};template

class ctestint2type_old

else}};

template

class ctestint2type

private:

void dosomething(int2typea)

void dosomething(int2typeb)

};void ctestthchniques::testint2typefun()

3、型別對型別的對映

// 通過模板函式過載,解決無法偏特化問題

// 但是過載會導致引數傳入的開銷,可以通過type2type解決

template

struct type2type;//

// 這是乙個重量級的引數,需要構造,拷貝,析構,

// 而它的作用只是根據型別來區分不同的函式過載

template

t* create_old(const u& arg, const t& type)

template

bool* create_old(const u& arg, const bool& type)

// 這是乙個輕量級的引數。但是它也足夠滿足區分不同函式過載的需求了

template

t* create(const u& arg, type2typetype)

template

bool* create(const u& arg, type2typetype)

void ctestthchniques::testtype2typefun()

4、型別選擇,根據引數選擇適當的型別

// 根據flag控制result型別,適合bool型別區控制型別

template

struct select

;template

struct select;//

template

struct testtypeselect

;5、編譯器檢測可轉換型和可繼承性,執行期檢測使用dynamic_cast

template

class cconversion

;static small test(u);

static big test(...);

static t maket();

public:

// 如果maket的型別為t能夠轉換為u,就會返回sizeof(small)了,這樣值就是1,不能轉換就會返回big了

// 依賴於編譯器的函式過載

enum

;enum;};

template

class cconversion

enum;};

// t是否繼承自u或則兩者是同一種型別,並且u不能使void*,因為任何型別都可以轉換成void*

// 加const是為了防止型別傳入const而轉換失敗

#define tisbasefromu(t, u)\

(cconversion::tcanconversionu)\

&& (!cconversion(const u* ,const void*>::sametype))

//class cconversion

//;//

//static small test(double);

//static big test(...);

//static int maket();

//public:

//enum;

//};

6、type_info

7、nulltype和emptytype

class nulltype;只有宣告,沒有定義,是乙個標記位置,不能使用的

struct emptytype{};

8、type traits

型別判斷,識別。通過模板特例化可判斷其型別,獲取其型別

template

class ctypetrains

;typedef nulltype pointeetype;

};template

struct pointertraits

;typedef u pointeetype;

};//

template

struct ptomtraits;};

//template

//struct ptomtraits

//;//};

//template

struct unconst

;template

struct unconst;//

template

struct refrencetraits;};

template

struct refrencetraits;};

public:

enum ;

enum ;

enum ;

typedef typename pointertraits::pointeetype pointeetype;

typedef typename unconst::result nonconsttype; // 去除const,如果t傳入的是const型別,而我們又需要申明t型別進行改變時的做法

};另外loki庫中的型別識別還支援函式指標型別識別,成員函式指標型別識別。這兩個語法比較特殊,舉例如下

函式指標

template

struct isfunctionpointerraw

;};template

struct isfunctionpointerraw

;};template

struct isfunctionpointerraw

;};template

struct isfunctionpointerraw

;};成員函式指標

template

struct ismemberfunctionpointerraw

;};template

struct ismemberfunctionpointerraw

;};template

struct ismemberfunctionpointerraw

;};template

struct ismemberfunctionpointerraw

;};// 有些引數是...型別的,如printf

template

struct ismemberfunctionpointerraw

;};template

struct ismemberfunctionpointerraw

;};template

struct ismemberfunctionpointerraw

;};// 有些引數是volatile型別。另外還有一些是const型別(函式型別是const)

template

struct ismemberfunctionpointerraw

;};template

struct ismemberfunctionpointerraw

;};// 是否是類的成員變數

template struct ptomtraits;};

template struct ptomtraits;};

template struct ptomtraits;};

使用舉例

void ctestthchniques::testtypetraits()



第二章,軟體測試技術

通過本章學習,你能學dao 軟體測試 測試是乙個以在程式被交付到終端使用者手上之前找到程式中的錯誤為目的的活動。測試是檢查產品的質量,而不是檢查開發人員得質量 為啥要測試?基於不同的立場,存在著兩種完全不同的測試目的。從使用者的角度的出發,普遍希望通過軟體測試暴露軟體中隱藏的 錯誤和缺陷,以考慮是否...

大資料技術第二章

hadoop簡介 hadoop的特性 hadoop的應用現狀 apache hadoop版本演變 hadoop各種版本 安裝linux虛擬機器 安裝雙作業系統 hadoop集群中有哪些節點型別 hadoop框架中最核心的設計是為海量資料提供儲存的hdfs和對資料進行計算的mapreduce mapr...

第二章 jQuery技術解密 四

jquery.fn.init 建構函式能夠構建 jquery 物件,並把匹配的 dom 元素儲存在 jquery 物件內部集合中。jquery.fn.init 建構函式可以接收單個的 dom 元素,也可以接收 dom 集合。如果接收的是字串型 id 值,則直接在文件中查詢對應的 dom 元素,並把它...