1.1 C++ stl_config.h 组态

本文详细介绍了C++ STL中的stl_config.h文件,包括__STL_STATIC_TEMPLATE_MEMBER_BUG、__STL_CLASS_PARTIAL_SPECIALIZATION、__STL_FUNCTION_TMPL_PARTILA_ORDER等关键配置,涉及类模板部分特例化、函数模板部分顺序、模板参数默认值等方面。

stl_config.h 组态

参考自《STL 源码剖析》

(1) __STL_STATIC_TEMPLATE_MEMBER_BUG

可以在类中定义全局变量,然后再类外根据参数定义值 ,定义类之后全局变量的值就是在类外定义的值。

template <typename T>
class test {
public:
	static int data;	
};
int test<int>::date = 1;
int test<char>::date = 2;

(3) __STL_CLASS_PARTIAL_SPECIALIZATION

使用template定义类时可以使用const *等做特殊设计。

template <class T>
struct test<T*, const T* > {....};

(3) __STL_FUNCTION_TMPL_PARTILA_ORDER

函数模板部分特例化,对于一个函数模板,如果定义了另一个函数模板且函数名相同,会根据template的参数表进行调用,要注意的是,第二个函数模板需要知道参数,可以在函数后面加上例如,max<T,T>或者函数之前声明类struct<T,T>.

template<typename T1, typename T2>
struct Bar
{
 void operator()(T1 const& t1, T2 const& t2)
 {
    std::cerr << "In Bar<T1, T2>(" << t1 << ", " << t2 << ")\n";
 }
};
template<typename T2>
struct Bar<int, T2>
{
 void operator()(int t1, T2 const& t2)
 {
    std::cerr << "In Bar<int, T2>(" << t1 << ", " << t2 << ")\n";
 }
};
/* 下列代码也可以
template<typename T2>
void operator()<int, T2>(int t1, T2 const& t2)
 {
    std::cerr << "In Bar<int, T2>(" << t1 << ", " << t2 << ")\n";
 }
 */
template<typename T1, typename T2>
void bar(T1 const& t1, T2 const& t2)
{
   Bar<T1, T2> b;
   b(t1, t2);
}

(4) __STL_EXPLICIT_FUNCTION_TMPL_ARGS

template可以嵌套template

template <class T>
class vector{
public:
	template <class TT> 
	void test(TT a,TT b) {
		cout << a << ' ' << b << endl; 
	}
};

(5) __STL_LIMITED_DEFAULT_TEMPLATES

template的参数可以根据前一个参数设定默认值(虽然觉得没啥用

template <class T, class Se = queue<T> >
class test {....};

(6) __STL_NON_TYPE_TMPL_RARAM_BUG

template 可以使用无参数类型模板

template<int size>
class CTest
{
        int m_data[size];
};

void main()
{
        CTest<10> obj;
}

(7) __STL_NULL_TMPL_ARGS(bound friend template friend)

友元约束模板 可以依据之前对非友元函数的定义来对友元函数进行约束
#define __STL_NULL_TMPL_ARGS <>

template<class T,class Sequence>
class stack;
 
template<class T,class Sequence>
bool operator==(const stack<T,Sequence>& x,const stack<T,Sequence>& y);

template<class T,class Sequence=deque<T> >
class stack
{
    //friend bool operator==<T>(const stack<T>&,const stack<T>&);
    //下面的都是等价于上面的
    //friend bool operator== <T>(const stack&,const stack&);
    friend bool operator== <>(const stack&,const stack&);
    Sequence c;
};
 template<class T,class Sequence>
bool operator==(const stack<T,Sequence> &x,const stack<T,Sequence> &y)
{
    return cout<<"operator=="<<'\t';
}
int main()
{
    stack<int> x;
    stack<int> y;
    cout<<(x==y)<<endl;
    
    stack<char> y1;
//    cout<<(x==y1)<<endl;
}

(8) __STL_TEMPLATE_NULL (class template explicit specialization)

对template进行具体化,在定义参数时就可以使用具体化的定义。函数同理。
#define __STL_TEMPLATE_NULL template<>

template<class T> struct test {....};
template<> struct test<int>{.....};

template<class Any>
void swap(Any &,Any &b){......;} 
template <> void swap<int>(int &,int &){......;} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值