操作系统:ubuntu10.04
STL源码版本:2.91
前言:
要看一个项目的源码,首先要选中切入点。
那么在sgi stl 标准库中,其切入点是什么呢?
答案是:stl_config.h 文件。
不同的编译器对C++语言的支持程度不尽相同。为了具备广泛移植能力,SGI STL 定义了一个环境组态文件
。
其中声明了许多宏定义,在预编译的时候,通过这些宏定义来编译出对于平台的程序。
1,stl_config.h在linux平台下的实现:
1.1)为了知道linux平台下sgi stl 的宏定义有那些是被定义了的,有个简单,直接的方法,直接输出其宏名字。
测试代码如下:
运行结果:
1.2)通过以上的测试,可以得到在linux平台下,stl_config.h 的实现定义如下:
为了缩小篇幅,把相关的版权信息的屏蔽了。
-
#ifndef __STL_CONFIG_H
-
# define __STL_CONFIG_H
-
-
-
# ifdef __GNUC__
-
# include <_G_config.h>
-
# define __STL_CLASS_PARTIAL_SPECIALIZATION
-
# define __STL_FUNCTION_TMPL_PARTIAL_ORDER
-
# define __STL_EXPLICIT_FUNCTION_TMPL_ARGS
-
# define __STL_MEMBER_TEMPLATES
-
# define __STL_PTHREADS
-
# define __STL_USE_EXCEPTIONS
-
# endif
-
-
-
# define __STL_NULL_TMPL_ARGS
-
# define __STL_TEMPLATE_NULL
-
-
-
# define __STD
-
# define __STL_BEGIN_NAMESPACE
-
# define __STL_END_NAMESPACE
-
# undef __STL_USE_NAMESPACE_FOR_RELOPS
-
# define __STL_BEGIN_RELOPS_NAMESPACE
-
# define __STL_END_RELOPS_NAMESPACE
-
# define __STD_RELOPS
-
-
-
# define __STL_TRY
-
# define __STL_CATCH_ALL if (false)
-
# define __STL_RETHROW
-
# define __STL_NOTHROW
-
# define __STL_UNWIND(action)
-
-
-
# define __stl_assert(expr)
-
-
#endif /* __STL_CONFIG_H */
-
-
// Local Variables:
-
// mode:C++
-
// End:
2,对stl_config.h中的宏的详解:
2.1)__STL_STATIC_TEMPLATE_MEMBER_BUG
-
-
-
#include <iostream>
-
using namespace std;
-
-
template <typename T>
-
class testclass
-
{
-
public:
-
static int _data;
-
};
-
-
template<> int testclass<int>::_data = 1;
-
template<> int testclass<char>::_data = 2;
-
-
int main()
-
{
-
cout<<testclass<int>::_data<<" "<<&testclass<int>::_data<<endl;
-
cout<<testclass<char>::_data<<" "<<&testclass<char>::_data<<endl;
-
-
testclass<int> obji1, obji2;
-
testclass<char> objc1, objc2;
-
-
cout<<obji1._data<<" "<<&obji1._data<<endl;
-
cout<<obji2._data<<" "<<&obji2._data<<endl;
-
cout<<objc1._data<<" "<<&objc1._data<<endl;
-
cout<<objc2._data<<" "<<&objc2._data<<endl;
-
-
obji1._data = 3;
-
objc2._data = 4;
-
-
cout<<obji1._data<<" "<<&obji1._data<<endl;
-
cout<<obji2._data<<" "<<&obji2._data<<endl;
-
cout<<objc1._data<<" "<<&objc1._data<<endl;
-
cout<<objc2._data<<" "<<&objc2._data<<endl;
-
-
return 1;
-
}
2.2)__STL_CLASS_PARTIAL_SPECIALIZATION
-
-
-
-
#include <iostream>
-
using namespace std;
-
-
template <class I, class O>
-
struct testClass
-
{
-
testClass()
-
{
-
cout<<"I, O"<<endl;
-
}
-
};
-
-
template <class T>
-
struct testClass<T*, T*>
-
{
-
testClass()
-
{
-
cout<<"T*,T*"<<endl;
-
}
-
};
-
-
template <class T>
-
struct testClass<const T*, T*>
-
{
-
testClass()
-
{
-
cout<<"const T*, T*"<<endl;
-
}
-
};
-
-
int main()
-
{
-
testClass<int, char> obj1;
-
testClass<int*, int*> obj2;
-
testClass<const int*, int*> obj3;
-
return 1;
-
}
2.3)__STL_FUNCTION_TMPL_PARTIAL_ORDER
-
-
-
2.4)__STL_EXPLICIT_FUNCTION_TMPL_ARGS
整个 SGI STL 内都没有用到此一常数定义
-
-
2.5)__STL_MEMBER_TEMPLATES
-
-
-
#include <iostream>
-
using namespace std;
-
-
class alloc
-
{
-
};
-
-
template <class T, class Alloc = alloc>
-
class vector
-
{
-
public:
-
typedef T value_type;
-
typedef value_type* iterator;
-
-
template <class I>
-
void insert(iterator position, I first, I last)
-
{
-
cout<<"insert()"<<endl;
-
}
-
};
-
-
int main()
-
{
-
int ia[5] = {0,1,2,3,4};
-
-
cout<<*ia<<endl;
-
cout<<*(ia+4)<<endl;
-
-
vector<int> x;
-
vector<int>::iterator ite = NULL;
-
x.insert(ite, ia, ia+4);
-
return 1;
-
}
2.6)__STL_LIMITED_DEFAULT_TEMPLATES
-
-
-
//测试template 参数可否根据前一个template参数而设定默认值
-
#include <iostream>
-
#include <cstddef>
-
-
using namespace std;
-
-
class alloc
-
{
-
};
-
-
template <class T, class Alloc = alloc, size_t BufSiz = 0>
-
class deque
-
{
-
public:
-
deque()
-
{
-
cout<<"deque"<<endl;
-
}
-
};
-
-
template <class T, class Sequence = deque<T> >
-
class stack
-
{
-
public:
-
stack()
-
{
-
cout<<"stack"<<endl;
-
}
-
-
private:
-
Sequence c;
-
};
-
-
int main()
-
{
-
stack<int> x;
-
}
2.7)__STL_NON_TYPE_TMPL_PARAM_BUG
-
-
-
//测试class template 可否拥有non-type template参数
-
#include <iostream>
-
#include <cstddef>
-
using namespace std;
-
-
class alloc
-
{
-
};
-
-
inline size_t __deque_buf_size(size_t n, size_t sz)
-
{
-
return n != 0 ? n : (sz < 512 ? size_t(512/sz) : size_t(1));
-
}
-
-
template <class T, class Ref, class Ptr, size_t BufSiz>
-
struct __deque_iterator
-
{
-
typedef __deque_iterator<T, T&, T*, BufSiz> iterator;
-
typedef __deque_iterator<T, const T&, const T*, BufSiz> const_iterator;
-
static size_t buffer_size()
-
{
-
return __deque_buf_size(BufSiz, sizeof(T));
-
}
-
};
-
-
template <class T, class Alloc = alloc, size_t BufSiz = 0>
-
class deque
-
{
-
public:
-
typedef __deque_iterator<T, T&, T*, BufSiz> iterator;
-
};
-
-
int main()
-
{
-
cout<<deque<int>::iterator::buffer_size()<<endl;
-
cout<<deque<int,alloc,64>::iterator::buffer_size()<<endl;
-
return 1;
-
}
参考文献:
1,《STL源码剖析》
2,http://blog.csdn.net/segen_jaa/article/details/7930349
阅读(4965) | 评论(1) | 转发(1) |