如何告诉编译器我的朋友函数是函数模板
How to tell the compiler my friend function is function template
这是我的代码:
#include <iostream>
#include <cstddef>
class alloc
{
};
template <class T, class Alloc = alloc, size_t BufSiz = 0>
class deque
{
public:
deque() { std::cout << "deque" << std::endl; }
};
template <class T, class Sequence = deque<T> >
class stack
{
public:
stack() { std::cout << "stack" << std::endl; }
private:
Sequence c;
friend bool operator== <> (const stack<T, Sequence>&, const stack<T, Sequence> &);
friend bool operator< <> (const stack<T, Sequence>&, const stack<T, Sequence>&);
};
template <class T, class Sequence>
bool operator== (const stack<T, Sequence>&x, const stack<T, Sequence>&y)
{
return std::cout << "opertor == " << std::endl;
}
template <class T, class Sequence>
bool operator < (const stack<T, Sequence> &x, const stack<T, Sequence> &y)
{
return std::cout << "operator <" << std::endl;
}
int main()
{
stack<int> x; // deque stack
stack<int> y; // deque stack
std::cout << (x == y) << std::endl; // operator == 1
std::cout << (x < y) << std::endl; // operator < 1
}
我只是想用一个简单的 <> 符号告诉编译器我的函数是函数模板。但是我得到错误,两个 line:friends can only be 类 or functions
friend bool operator== <> (const stack<T, Sequence>&, const stack<T, Sequence> &);
friend bool operator< <> (const stack<T, Sequence>&, const stack<T, Sequence>&);
我该如何解决。
只需使用以下语法:
template<typename T1, typename Sequence1>
friend bool operator== (const stack<T1, Sequence1>&, const stack<T1, Sequence> &);
template<typename T1, typename Sequence1>
friend bool operator< (const stack<T1, Sequence1>&, const stack<T1, Sequence>&);
您需要第一个模板参数不同于 T,第二个模板参数不同于 Sequence,否则您将隐藏 class' 模板参数。
这是我的代码:
#include <iostream>
#include <cstddef>
class alloc
{
};
template <class T, class Alloc = alloc, size_t BufSiz = 0>
class deque
{
public:
deque() { std::cout << "deque" << std::endl; }
};
template <class T, class Sequence = deque<T> >
class stack
{
public:
stack() { std::cout << "stack" << std::endl; }
private:
Sequence c;
friend bool operator== <> (const stack<T, Sequence>&, const stack<T, Sequence> &);
friend bool operator< <> (const stack<T, Sequence>&, const stack<T, Sequence>&);
};
template <class T, class Sequence>
bool operator== (const stack<T, Sequence>&x, const stack<T, Sequence>&y)
{
return std::cout << "opertor == " << std::endl;
}
template <class T, class Sequence>
bool operator < (const stack<T, Sequence> &x, const stack<T, Sequence> &y)
{
return std::cout << "operator <" << std::endl;
}
int main()
{
stack<int> x; // deque stack
stack<int> y; // deque stack
std::cout << (x == y) << std::endl; // operator == 1
std::cout << (x < y) << std::endl; // operator < 1
}
我只是想用一个简单的 <> 符号告诉编译器我的函数是函数模板。但是我得到错误,两个 line:friends can only be 类 or functions
friend bool operator== <> (const stack<T, Sequence>&, const stack<T, Sequence> &);
friend bool operator< <> (const stack<T, Sequence>&, const stack<T, Sequence>&);
我该如何解决。
只需使用以下语法:
template<typename T1, typename Sequence1>
friend bool operator== (const stack<T1, Sequence1>&, const stack<T1, Sequence> &);
template<typename T1, typename Sequence1>
friend bool operator< (const stack<T1, Sequence1>&, const stack<T1, Sequence>&);
您需要第一个模板参数不同于 T,第二个模板参数不同于 Sequence,否则您将隐藏 class' 模板参数。