__STL_NULL_TMPL_ARGS

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

<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值