[C++] Equality operator overloading: friend member functions

Started by
3 comments, last by johnstanp 13 years, 11 months ago
According to this page, the following code should compile perfectly fine:
template <typename T>
class Vector
{
public:
    friend bool operator==<T> (const Vector<T> &v1, const Vector<T> &v2);
};

template <typename T>
inline
bool operator==(const Vector<T> &v1, const Vector<T> &v2)
{
}
However, GCC 4.4.0 complains right at the declaration:
D:\C\Performance\main.cpp:19: error: declaration of 'operator==' as non-function
D:\C\Performance\main.cpp:19: error: expected ';' before '<' token
Why does this not work? Must I use a new template parameter just for the function? Also, should all equality / inequality / shift operators be defined as friend "symmetric" functions rather then "asymmetric" member functions? Are there exceptions? Thank you!
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Advertisement
Quote:Original post by Decrius
According to this page, the following code should compile perfectly fine:

*** Source Snippet Removed ***

However, GCC 4.4.0 complains right at the declaration:

D:\C\Performance\main.cpp:19: error: declaration of 'operator==' as non-functionD:\C\Performance\main.cpp:19: error: expected ';' before '<' token


Why does this not work? Must I use a new template parameter just for the function?
Did you forward declare the Vector class template and the operator==() function?
Quote:Original post by jyk
Quote:Original post by Decrius
According to this page, the following code should compile perfectly fine:

*** Source Snippet Removed ***

However, GCC 4.4.0 complains right at the declaration:

D:\C\Performance\main.cpp:19: error: declaration of 'operator==' as non-functionD:\C\Performance\main.cpp:19: error: expected ';' before '<' token


Why does this not work? Must I use a new template parameter just for the function?
Did you forward declare the Vector class template and the operator==() function?


Oh, that's really silly...(A)
The other question remains though :)
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by Decrius
Also, should all equality / inequality / shift operators be defined as friend "symmetric" functions rather then "asymmetric" member functions? Are there exceptions?


Generally, I would say yes. This allows implicit conversions (via constructor or conversion operator) for the left hand argument, just as with the right hand side. Consider:
const char* cstring = "foo";std::string cppstring = "bar";std::string a = cppstring + cstring; // works always, uses std::string::string (const char*)std::string b = cstring + cppstring; // won't work unless operator+ is "symmetric"


Not all operators can be done this way (e.g. assignment operators), so obviously those are out, but it generally can't hurt where it's possible.

EDIT: Also, I should share this cheap trick:
template <typename T>class Vector{public:    friend bool operator==(const Vector<T> &v1, const Vector<T> &v2) {        ...    }};

Also works [lol]
template <typename T>class Vector{    friend bool operator==<T> (const Vector<T> &v1, const Vector<T> &v2);    public:    private:    };template <typename T>inlinebool operator==(const Vector<T> &v1, const Vector<T> &v2){}


...is better to convey the notion that friendship has nothing to do with accessibility in a class.

This topic is closed to new replies.

Advertisement