when must/not namespaces be declared in headers?

When I write a header containing only the prototypes of functions with distinctive names, it works without my creating a special namespace, as long as I include the header in my implementation file. But it seems that when the header contains a class, we are expected to create a namespace as a matter of good practice. Questions:

1. Is the creation of a namespace primarily a housekeeping issue, to help avoid redundancies in the global scope?

2. Are there situations, apart from avoiding redundancy in the names of entities, when creating a namespace is definitely required? Is it always required with overloaded operators, for instance?

3. Are there situations in which creating a namespace is definitely wrong?

Thanks!
You are never required to put any declarations whatsoever in your own namespace.

It is good practice, in a large project, to put every declaration in a namespace. Maybe you have one namespace,
maybe two, maybe many. That's up to name. Namespaces were created to avoid name collisions between
two or more independent libraries. For example, you might download someone's libfoo and someone else's
libbar, but when you go to link both of them into your executable, you get duplicate definition errors because
both libraries had the function void init_frobnicator();.

So the answer to 1 is yes... it is meant to avoid symbol collisions.

I'm not sure about 2. Certainly you never need to create your own namespace. I'm not sure if it is necessary
to extend the std namespace to satisfy certain lookup rules. But I can say that it doesn't make sense to have
operators in a namespace. For example, I don't know what it means to declare
1
2
Rational operator+( Rational lhs,
Rational rhs )
in namespace rat. Does that mean in order to write r1 + r2 (both being of type Rational) I
have to use the namespace?

The answer to 3 is no.
1) a namespace is not to avoid redundancies, it's to avid ambiguity between symbols with the same name
2+3 ) as anything there are no situation in which it's definitely required or wrong
Topic archived. No new replies allowed.