• Advertisement
Sign in to follow this  

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

This topic is 2823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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-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?
Did you forward declare the Vector class template and the operator==() function?

Share this post


Link to post
Share on other sites
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-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?
Did you forward declare the Vector class template and the operator==() function?


Oh, that's really silly...(A)
The other question remains though :)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

template <typename T>
class Vector
{
friend bool operator==<T> (const Vector<T> &v1, const Vector<T> &v2);

public:

private:

};

template <typename T>
inline
bool 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement