1. 程式人生 > 實用技巧 >STL原始碼剖析——SGI STL編譯器組態

STL原始碼剖析——SGI STL編譯器組態

1、__STL_STATIC_TEMPLATE_MEMBER_BUG

如果編譯器無法處理static member of template classes(模板類靜態成員)就定義

2、__STL_CLASS_PARTIAL_SPECIALIZATION

如果編譯器支援 partial specialization of class templates(模板類偏特化)就定義,所謂模板類偏特化

參考:

  http://blog.csdn.net/thefutureisour/article/details/7964682/

  https://blog.csdn.net/q8250356/article/details/80672466

3、__STL_FUNCTION_TMPL_PARTIAL_ORDER

如果編譯器支援partial ordering of function templates或者說partial specialization of function templates就定義。

參考:

  https://msdn.microsoft.com/en-us/library/zaycz069.aspx 可以理解為對函式模板的過載的支援

  https://blog.csdn.net/q8250356/article/details/80672500

4、__STL_MEMBER_TEMPLATES

如果編譯器支援template members of classes 就定義,模板類中包含模板成員

5、__STL_LIMITED_DEFAULT_TEMPLAES

用到前一個模板的模板形參的某一個具現體作為當前模板的模板形參的預設值

例如:
template<class T, class Sequence=deque<T> >

6、__STL_NON_TYPE_TMPL_PARAM_BUG

測試類模板是否使用非型別模板引數(non-type template parameters)
參考:

  http://blog.csdn.net/zhangxiao93/article/details/50747862

  https://blog.csdn.net/q8250356/article/details/80672544

7、__STL_NULL_TMPL_ARGS

__STL_NULL_TMPL_ARGS是定義在 <stl_config.h>,定義如下:

1 # ifdef __STL_EXPLICIT_FUNCTION_TMPL_ARGS 
2 # define __STL_NULL_TMPL_ARGS <> 
3 # else 
4 # define __STL_NULL_TMPL_ARGS 
5 # endif

在STL中的<stl_stack.h>中有出現

 1 template <class T, class Sequence = deque<T> >
 2 class stack {
 3   friend bool operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);
 4   friend bool operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);
 5   //......
 6  };
 7  //也就是:
 8  template <class T, class Sequence = deque<T> > 
 9 class stack { 
10 friend bool operator== <> (const stack&, const stack&); 
11 friend bool operator< <> (const stack&, const stack&); 
12 //... 
13 };
14 
15 //實現:
16 template <class T, class Sequence>
17 bool operator==(const stack<T, Sequence>& x, const stack<T, Sequence>& y) {
18   return x.c == y.c;
19 }
20 
21 template <class T, class Sequence>
22 bool operator<(const stack<T, Sequence>& x, const stack<T, Sequence>& y) {
23   return x.c < y.c;
24 }

下面這四種寫法都是可以的:

 1 //第一種寫法:
 2 friend bool operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);
 3 friend bool operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);
 4 //第二種寫法:
 5 friend bool operator== <T> (const stack<T>&, const stack<T>&); 
 6 friend bool operator< <T> (const stack<T>&, const stack<T>&); 
 7 //第三種寫法:
 8 friend bool operator== <T> (const stack&, const stack&); 
 9 friend bool operator< <T> (const stack&, const stack&); 
10 //第四種寫法:
11 friend bool operator== <> (const stack&, const stack&); 
12 friend bool operator< <> (const stack&, const stack&);

但是不能寫成這樣:

1 //不可以
2 // friend bool operator== (const stack&, const stack&); 
3 // friend bool operator< (const stack&, const stack&);

8、__STL_TEMPLATE_NULL

即 template <> 顯式的模板特化
比如
  __STL_TEMPLATE_NULL struct hash<char>{...};
展開為
  template<> struct hash<char>{...};