在C++中我们可以通过typeid来获取一个类型的名称(内置类型和自定义类型都可以),但是我们不能用这种方式获取来的名称做变量的声明。在C++中我们可以通过类型萃取的方式来区分内置类型和自定义类型。
例如:我们在Seqlist中要用到类型萃取,因为内置类型我们可以通过memcopy和memmove这两个方式进行拷贝,自定义类型或string我们要通过赋值的方式拷贝。这样的话会提高效率。
类型萃取的方式:类型萃取是在模板的基础上区分内置类型和其他类型,主要原理是将内置类型全部特化,然后再进行区分。
struct TrueType
{
bool Get()
{
return true;
}
};
struct FalseType
{
bool Get()
{
return false;
}
};
template<typename T>
struct TypeTraits
{
typedef FalseType IsPODType; //如果不是内置类型,则IsPODType是FalseType
};
//将所有内置类型特化
template<>
struct TypeTraits<int>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<unsigned int>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<short>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<unsigned short>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<char>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<unsigned char>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<float>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<double>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<long>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<long double>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<unsigned long>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<long long>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<>
struct TypeTraits<unsigned long long>
{
typedef TrueType IsPODType; //如果是内置类型则IsPODType是TrueType
};
template<typename T>
void Copy(T* dest,T* src,int sz)
{
if (TypeTraits<string>::IsPODType().Get() == 1)//如果是内置类型
{
memmove(dest, src, sz*sizeof(T)); //对于string存在浅拷贝的问题,所以string必须用值拷贝
}
else //如果不是内置类型
{
for (int i = 0; i < sz; i++)
{
dest[i] = src[i];
}
}
}
类型萃取的技巧:把自定义类型和内置类型的区分开,然后对自定义类型的使用for循环拷贝,对于内置类型的,采用memcpy的方式进行拷贝。
使用类型萃取的原因
当顺序表是自定义类型,进行顺序表增容的时候,这个时候会出现一个问题,比如string类型,这个类型中有一个_buf与_ptr,当储存少于16个的时候这时会储存在_buf当中的,如果多于16个,那个会单独开辟空间,进行储存,这时拷贝的时候就是拷贝过去这个储存的地址而已,所以这样调用析构函数的时候,当增加容量的时候,这个时候会把储存string的那块空间进行释放。会造成数据丢失的问题。
所以,在这里面我们提到一个类型萃取的技巧,可以把自定义类型和内置类型的区分开,然后对自定义类型的使用for循环拷贝,对于内置类型的,采用memcpy的方式进行拷贝。
#include<iostream>
#include<stdlib.h>
using namespace std;
//类型萃取
struct __truetype
{
bool get()
{
return true;
}
};
struct __falsetype
{
bool get()
{
return false;
}
};
template<typename T>
struct typetraits
{
typedef __falsetype __ispodtype;
};
template<>
struct typetraits<int >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<char >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<short >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<bool >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<unsigned int >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<unsigned short >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<unsigned long >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<long >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<long long >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<unsigned long long >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<long double >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<double >
{
typedef __truetype __ispodtype;
};
template<>
struct typetraits<float >
{
typedef __truetype __ispodtype;
};
template<typename T>
void Copy(const T*src, T* dst, size_t size)
{
if (typetraits<T>::__ispodtype().get())
{
cout << "__truetype:" << typeid(T).name() << endl;
memcpy(dst, src, size*sizeof(T));
}
else
{
cout << "__falsetype:" << typeid(T).name() << endl;
for (size_t i = 0; i < size; i++)
{
dst[i] = src[i];
}
}
}
void test()
{
int a1[8] = { 1, 2, 3, 4, 5, 6 };
int a2[8] = { 9, 5, 6, 7, 8, 2, 2 };
Copy(a1, a2, 3);
cout << a2<<endl;
string c1[10] = {"123","7989465","456321","4561","4563"};
string c2[5] = {"654","312","a"};
Copy(c1, c2, 3);
}
int main()
{
test();
system("pause");
return 0;
}
在这里我们采用了模板的特化,这里使用了全特化,直接给int,double等内置类型做特化。
在这里我们设定内置类型是truetype,设置自定义类型是falsetype,然后我们通过不同的类型利用get()函数返回不同的布尔值,这样对内置类型采用memcp拷贝,对于非内置类型,采用for循环拷贝,这样就能实现我们想要的结果了。