Reputation: 87
I want to add some friend declarations to a class. For example, I want to add some functions of operator==
,operator <
. So what I have to do is use the forward declarations:
template <typename >
class MyBlob;
template <typename T>
bool operator==(const MyBlob<T> &, const MyBlob<T>&);
template <typename T>
bool operator!=(const MyBlob<T> &, const MyBlob<T>&);
template <typename T>
bool operator<(const MyBlob<T> &, const MyBlob<T>&);
template <typename T>
class MyBlob
{
friend bool operator== <T>(const MyBlob<T> &lhs,const MyBlob<T> &rhs);
friend bool operator!= <T>(const MyBlob<T> &lhs,const MyBlob<T> &rhs);
friend bool operator< <T>(const MyBlob<T> &lhs,const MyBlob<T> &rhs);
//other things
};
This is annoying that I have to use template <typename T>
for three times. And this really reduce the readability.
So, is there any method to make the forward declaration more simple? Or can I have some method to declare these things in one place just like the ordinary function?
If this can't be done, Is using typedef
to simplify the template <typename T>
a good idea?
Upvotes: 0
Views: 92
Reputation: 536
You can try defining friend operators within the class declaration:
template <typename T>
class MyBlob {
friend bool operator== (const MyBlob& lhs, const MyBlob& rhs) {
// ...
}
friend bool operator!= (const MyBlob& lhs, const MyBlob& rhs) {
// ...
}
friend bool operator< (const MyBlob& lhs, const MyBlob& rhs) {
// ...
}
// ...
};
Upvotes: 1