文章目录
- stl_config.h 组态
- (1) __STL_STATIC_TEMPLATE_MEMBER_BUG
- (3) __STL_CLASS_PARTIAL_SPECIALIZATION
- (3) __STL_FUNCTION_TMPL_PARTILA_ORDER
- (4) __STL_EXPLICIT_FUNCTION_TMPL_ARGS
- (5) __STL_LIMITED_DEFAULT_TEMPLATES
- (6) __STL_NON_TYPE_TMPL_RARAM_BUG
- (7) __STL_NULL_TMPL_ARGS(bound friend template friend)
- (8) __STL_TEMPLATE_NULL (class template explicit specialization)
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 &){......;}

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

被折叠的 条评论
为什么被折叠?



