在模板类中编写朋友函数声明的正确方法是什么? [英] What is the right way to write friend function declarations in template class?
问题描述
我正在尝试编写自己的矢量模板类,但是在编写朋友函数声明时遇到了一些问题.
I'm trying to write my own vector template class, but I have some problems when writing friend function declarations.
起初我是这样写的:
template <typename T, typename Alloc = std::allocator<T>>
class vector {
public:
friend bool operator==(const vector<T, Alloc>&, const vector<T, Alloc>&);
};
但是编译器报告警告,我声明了一个非模板函数.因此,我将朋友声明更改为:
But the compiler reports a warning that I declare a non-template function. So I changed the friend declaration to this:
template <typename T, typename Alloc = std::allocator<T>>
class vector {
public:
template <typename E, typename F>
friend bool operator==(const vector<E, F>&, const vector<E, F>&);
};
到目前为止,一切都很好,但我认为仍然存在问题.如果我这样写,我将使用所有以两个模板参数作为其朋友功能的operator==
函数.例如,operator==(const vector<int>&, const vector<int>&)
和operator==(const vector<double>&, const vector<double>&)
都将是vector<int>
的朋友功能.
So far everything is fine, but I think there are still problems. If I write like that, I make all operator==
functions which take two template arguments as its friend functions. For example, operator==(const vector<int>&, const vector<int>&)
and operator==(const vector<double>&, const vector<double>&)
would both be vector<int>
's friend function.
在模板类中编写朋友函数的正确方法是什么?
What is the right way to write friend functions in template class?
推荐答案
朋友非模板功能
但是编译器报告警告,我声明了一个非模板函数.
But the compiler reports a warning that I declare a non-template function.
是的,您要在类定义中声明一个非模板函数.这意味着,如果您在类定义之外定义它,则必须将其定义为非模板函数,并针对所有可能的实例进行定义,例如:
Yes, you're declaring a non-template function inside the class definition. That means if you define it out of the class definition, you have to define it as non-template function, and for all the possible instantiations , like:
bool operator==(const vector<int>& v1, const vector<int>& v2)
{
...
}
bool operator==(const vector<char>& v1, const vector<char>& v2)
{
...
}
这很丑陋,您可以在类定义中定义它,例如
That is ugly, you can define it inside the class definition like
template <typename T, typename Alloc = std::allocator<T>>
class vector {
public:
friend bool operator==(const vector<T, Alloc>&, const vector<T, Alloc>&) {
...
}
};
朋友功能模板
如果要将其定义为模板函数,并限制友谊范围,则可以
Friend function template
If you want to define it as template function, and constrain the scope of friendship, you can
// forward declaration
template <typename T, typename Alloc>
class vector;
// forward declaration
template <typename T, typename Alloc>
bool operator==(const vector<T, Alloc>& v1, const vector<T, Alloc>& v2);
template <typename T, typename Alloc = std::allocator<T>>
class vector {
private:
int i;
public:
// only the instantiation of operator== with template parameter type of current T and Alloc becomes friend
friend bool operator==<>(const vector<T, Alloc>& v1, const vector<T, Alloc>& v2);
};
template <typename T, typename Alloc = std::allocator<T>>
bool operator==(const vector<T, Alloc>& v1, const vector<T, Alloc>& v2)
{
...
}
然后,对于vector<int>
,只有bool operator==(const vector<int>&, const vector<int>&)
是朋友,而其他实例化(例如bool operator==(const vector<double>&, const vector<double>&)
)则不是.
Then, for vector<int>
, only bool operator==(const vector<int>&, const vector<int>&)
is friend, other instantiations like bool operator==(const vector<double>&, const vector<double>&)
is not.
这篇关于在模板类中编写朋友函数声明的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!