C++模板特化 specialization

特化是泛化的反面,针对特定的具体的设计的一种方法:

//泛化
template 
struct hash{};


//特化

template<>
struct hash
{
    size_t operator()(char x)const  {return x;}
}

template<>
struct hash
{
    size_t operator()(char x)const  {return x;}
}


template<>
struct hash
{
    size_t operator()(char x)const  {return x;}
}

//使用

cout<()(1000);

//hash() 生成临时对象

模板偏特化  --个数偏

template
class vector
{
......
}


//根据需要,如果只是需要一个boolean 值,可以使用T 绑定bool类型

template

class vector
{
    .....
}

模板偏特化---范围偏

//泛化
template 
class C
{
    ....
};

//特化
tempalte 
class C
{
    ...
};


//C++支持比如你要用一种类型T指向一种东西,可以特化使用这种类型的指针,至于指向什么东西,都可以

 

你可能感兴趣的:(C++基础总结)