寫乙個高效且通用的拷貝函式

2021-07-29 05:23:25 字數 2213 閱讀 4077

通用的拷貝函式呢,對於大部分內建型別來說,都是可以直接拷貝的,我們呼叫memcpy()就可以直接完成,並且效率也很高。

但是出現類似於string類時,直接用mempy()就會出現淺拷貝的問題。這時,我們就需要對各種型別進行選擇,選擇合適的拷貝方式。

【思路】:首先,我們會考慮下多傳乙個引數進行判斷一下,這樣很容易忘記,有沒有更好的辦法呢?

接下來我們可能會選擇定義乙個對應型別的函式:-》對於任何型別我們都需要進行定義一下,這樣似乎也不合適

那我們就來考慮一下型別萃取,只需要定義一部分,就可以完成對所有型別的檢測。

接下類,我們再來看一下c++中資料型別:

//實現對不同的型別都能進行高效正確的拷貝

//模板

struct __falsetype

};struct __truetype

};template

struct typetraits

;//對內建型別進行特化

template

<>

struct typetraits

;template

<>

struct typetraits

;template

<>

struct typetraits

char>

;template

<>

struct typetraits

;template

<>

struct typetraits

short>

;template

<>

struct typetraits

;template

<>

struct typetraits

int>

;template

<>

struct typetraits

;template

<>

struct typetraits

long>

;template

<>

struct typetraits

long>

;template

<>

struct typetraits

long

long>

;template

<>

struct typetraits

;template

<>

struct typetraits

;template

<>

struct typetraits

double>

;template

struct typetraits<_tp>

;//使用引數推導的型別萃取

寫乙個記憶體拷貝函式

include using namespace std void mymemcpy void out pdst,void in psrc,int in ilen if pdest psrc pdest cout the dest point is large than src else cout t...

寫乙個通用的事件偵聽器函式

markyun.event var oldonload window.onload if typeof window.onload function else 視能力分別使用dom0 dom2 ie方式 來繫結事件 引數 操作的元素,事件名稱 事件處理程式 addevent function ele...

寫乙個通用的事件偵聽器函式

event 事件 工具集 markyun.event var oldonload window.onload if typeof window.onload function else 視能力分別使用dom0 dom2 ie方式 來繫結事件 引數 操作的元素,事件名稱 事件處理程式 addevent...