Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should non-member operator overloads be placed?

I want to overload operator<< for my class. Should I add this overloaded definition to the std namespace? (since the ostream operator<< is part of the std namespace) Or should I just leave it in the global namespace?

In short:

class MyClass {

};

namespace std {
    ostream& operator<< ( ostream& Ostr, const MyClass& MyType ) {}
}

OR

class MyClass {

};

std::ostream& operator<< ( std::ostream& Ostr, const MyClass& MyType ) {}

Which is more appropriate and why? Thanks in advance for your responses.

like image 656
Praetorian Avatar asked Sep 02 '10 03:09

Praetorian


People also ask

When should you consider using non-member overload operators for your operators?

The rules are applied in order. An overloaded operator will be used if either of the two rules is satisfied. When operator overloading is achieved using non-member function there are two cases to be considered: the overloaded operator uses only the public interface of the class(es) involved in the overloading, or.

Why is the overload not a member function?

Not everything can be overloaded as a member function Because the overloaded operator must be added as a member of the left operand. In this case, the left operand is an object of type std::ostream. std::ostream is fixed as part of the standard library.

Where do we need operator overloading?

The purpose of operator overloading is to provide a special meaning of an operator for a user-defined data type. With the help of operator overloading, you can redefine the majority of the C++ operators. You can also use operator overloading to perform different operations using one operator.

Which operator is required to be overloaded as member function only?

Explaination. Overloaded assignment operator does the job similar to copy constructor and is required to be overloaded as member function of the class.


3 Answers

You should put the operator overload in the same namespace as your class.

This will allow the operator to be found during overload resolution using argument-dependent lookup (well, actually, since ostream is in namespace std, the overload overload would also be found if you put it in namespace std, but there is no reason to do that).

From the point of view of good design practices, the operator overload is more a part of your class's interface than the interface of ostream, so it belongs in the same namespace as your class (see also Herb Sutter's Namespaces and the Interface Principle).

From the point of view of writing standards-compliant and portable code, you can't put the operator overload into namespace std. While you can add template specializations for user-defined entities to namespace std, you can't add additional function overloads.

like image 105
James McNellis Avatar answered Oct 25 '22 09:10

James McNellis


Don't add it to the std namespace, place it in the same namespace as your class. The purpose of a namespace is to prevent collisions. The standard says

17.4.3.1 Reserved names

It is undefined for a C++ program to add declarations or definitions to namespace std or namespaces within namespace std unless otherwise specified. A program may add template specializations for any standard library template to namespace std. Such a specialization (complete or partial) of a standard library template results in undefined behavior unless the declaration depends on a user-defined name of external linkage and unless the specialization meets the standard library requirements for the original template.

like image 38
Sam Miller Avatar answered Oct 25 '22 09:10

Sam Miller


Don't add to the standard namespace. Reason : If everybody did this, the standard namespace would have heaps of name clashes, which defeats the purpose of a namespace.

Your objective is for your class to be "ostream-able". It does not need to be in the standard namespace to do that. As long as it is in whatever namepsace your class is declared in, you're fine. Putting it in the standard namespace would be bad practice.

like image 41
Carl Avatar answered Oct 25 '22 09:10

Carl