Where should non-member operator overloads be placed?
Asked Answered
K

3

17

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.

Kane answered 2/9, 2010 at 3:26 Comment(0)
L
30

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.

Lozano answered 2/9, 2010 at 3:30 Comment(3)
Is the last sentence really correct considering Sam's response and quote from the Standard.Aegeus
@chubsdad: Yes, I think so: You can add specializations; you can't add overloads.Lozano
What if you want to add an overload for a class in std (e.g., std::nullptr_t, std::unique_ptr<>, etc.)?Ottie
L
7

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.

Locris answered 2/9, 2010 at 3:31 Comment(0)
W
6

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.

Wakerobin answered 2/9, 2010 at 3:30 Comment(1)
It defeats the purpose of the std namespace, which is for C++ standard library code. Which user code is not.Sofko

© 2022 - 2024 — McMap. All rights reserved.