__STL_NULL_TMPL_ARGS

来源:互联网 发布:mysql 唯一约束语句 编辑:程序博客网 时间:2024/06/01 18:50

类模板的模板友元函数定义

<span style="font-family:Courier New;">//in <stl_config.h># ifdef __STL_EXPLICIT_FUNCTION_TMPL_ARGS#   define __STL_NULL_TMPL_ARGS <># else#   define __STL_NULL_TMPL_ARGS# endif例:  //in <stl_stack.h>template <class T, class Sequence = deque<T> >class stack {  friend bool operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);  friend bool operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);...};</span>

<span style="font-family:Courier New;">//测试程序#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" << ' ';    }};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<T>&, const stack<T>&);       friend bool operator== <T> (const stack&, const stack&);    friend bool operator< <T> (const stack&, const stack&);    friend bool operator== <> (const stack&, const stack&);    friend bool operator< <> (const stack&, const stack&);    */    /*可行的写法*/    friend bool operator== <T, Sequence> (const stack&, const stack&);    friend bool operator< <T, Sequence> (const stack&, const stack&);    /*错误写法    friend bool operator== (const stack&, const stack&);    friend bool operator< (const stack&, const stack&);    */   public:    stack() {        cout << "stack" << endl;    }private:    Sequence c;};template <class T, class Sequence>bool operator== (const stack<T, Sequence>& x, const stack<T, Sequence>& y) {    cout << "operator==" << '\t';    return true;}template <class T, class Sequence>bool operator< (const stack<T, Sequence>& x, const stack<T, Sequence>& y) {    cout << "operator<" << '\t';    return true;}int main() {    stack<int> x;    stack<int> y;    cout << (x == y) << endl;    cout << (x < y) << endl;    //stack<char> y1;    //cout << (x == y1) << endl;    //cout << (x < y1) << endl;}</span>

http://www.cppblog.com/zerolee/archive/2010/11/03/132344.html

当模板函数被声明为类模板的友元且定义在类模板之外时,在函数名之后必须紧跟模板实参表,用来代表该友元声明指向函数模板的实例。否则友元函数会被解释为一个非模板函数,链接时无法解析。
友元模板函数的模板参数类型,并不一定要求是类模板的参数类型,也可以另外声明。



0 0
原创粉丝点击