Sign in to follow this  
Decrius

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

Recommended Posts

Decrius    100
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
jyk    2094
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
Decrius    100
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
MaulingMonkey    1730
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
johnstanp    267

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this