tags:

views:

1900

answers:

7

Just teaching myself c++ namespaces (coming from a c# background) and i'm really starting to think that all the things that c++ does better then most other languages, nested namespaces isn't one of them!

Am i right in thinking that in order to declare nested namespaces i HAVE to do the following:

namespace tier1
{
    namespace tier2
    {
        namespace tier3
        {
            /* then start your normal code nesting */
        }
    }
}

as apposed to:

namespace tier1::tier2::tier3
{
}

ala c#.

This becomes even more demented when i need to forward declare:

namespace tier1
{
    namespace tier2
    {
        namespace forward_declared_namespace
        {
            myType myVar; //forward declare
        }
        namespace tier3
        {
            /* then start your normal code nesting */
            class myClass
            {
                forward_declared_namespace::myType myMember;
            }
        }
    }
}

Bearing in mind that a typical system that i develop consists of:

MyCompany::MySolution::MyProject::System::[PossibleSections]::Type

Is this why you don't tend to see much use of namespaces in c++ examples? or usually on single (un-nested) namespaces?

+7  A: 

C++ namespaces were a vast improvement over the previous offering (i.e. No namespaces at all).

C# namespaces have extended the concept and ran with it.

I would advise you to keep your namespaces in a simple flat structure.

EDIT

Simply "Yes"

C++ namespaces weren't designed to help you partition your logic & libraries the way they do in C#.

The pruposes of C++ namespaces are to stop the real world problem encountered by C developers, where they're using two third party libs that export the same function name(s). C developers had various workarounds for it, but it could be a serious pain.

The idea was that the STL etc has the std:: namespace, libs provided by "XYZ Corp" would have an xyz:: namespace, you working for "ABC corp" would put ALL your stuff in a single abc:: namespace.

Binary Worrier
Do you advise that due to the short comings i've outlined here?
Adam Naylor
+1  A: 

First of all you can avoid namespace indentation, because there is no reason for that.

Using namespaces in the examples won't show namespaces power. And their power as for me is dividing Domain areas one from another. Divide utility classes from the business ones.

Just don't mix different namespace hierarchies in the one .h file. Namespaces are kind of extra comment for your interface of functions declaration. Looking on namespaces and the class names should explain a lot of stuff.

namespace product
{
namespace DAO
{

class Entity
{
};
Mykola Golubyev
"First of all you can avoid namespace indentation, because there is no reason for that." that's exactly what i actually do ;)
Adam Naylor
+14  A: 

C++ namespaces were not intended to be a design mechanism - they are there simply to prevent name clashes. You really don't want or need to use nested namespaces in 99.99% of situations.

A good example of the correct use of namespaces in C++ is the C++ Standard Library. Everything in this quite large library is placed in a single namespace called std - there is no attempt or need to break the library up into (for example) an I/O sub-namespace, a math sub-namespace, a container sub-namespace etc.

The basic tool for modelling in C++ is the class (and to some extent the template), not the namespace. If you feel the need for nesting, you should consider using nested classes, which have the following advantages over namespaces:

  • they have methods
  • they can control access
  • they cannot be re-opened

Having considered these, if you still wish to use nested namespaces by all means do so - there is nothing technically wrong with using them in this way.

anon
templates were not intended to be a mpl mechanism. But Guys use them in that way.
Mykola Golubyev
Good advice as always mr. butterworth!
Adam Naylor
Is it a good to name classes like DAOUser DAOPacient VIEWUser MODELUser, etc.
Mykola Golubyev
Eh . . . I hate to be a pedant (we all know that's a lie), but doesn't <iostream> implement an ios namespace inside the std namespace? I don't have a C++ compiler handy at the moment . . .
Binary Worrier
Could you provide arguments (not examples), why is not good idea use nested namesapces?
bb
@bb I believe in examples - they usually are the result of real world experience
anon
@binary ios is a typedef for basic_ios<char>
anon
@Neil - I saw more examples without namespaces, with just one namespace, with nested namespace. I can't chose right way only by examples.. I need to see all pros and cons.
bb
look at the boost at least. It is a library. And in the project there is can be library and the math module and ORM module and RPC module and other modules with the Data related to those parts.
Mykola Golubyev
@bb if I were to make one argument against it is that the world is not made up of things that split naturally into nested categories - hence the many failures of Linneaen taxonomy. Software isn't made that way either, IMHO.
anon
@mykola - why didn't you make that point in your answer to the question?
anon
@Neil: I don't know. I fill in the way that namespaces are design instrument. It is hard explain how you do breath when you breath every moment.
Mykola Golubyev
@Neil: I stand corrected. +1 mate for an excellent answer
Binary Worrier
"you should consider using nested classes" and beside all those pros you mentioned they have one con - the nesting, which is I suppose essence of this question, will look EXACTLY like in Naylor's example...
doc
C++ has a `rel_ops` namespace in `std`, and C++0x adds a few others.
Johannes Schaub - litb
Namespaces in C++ have specific features in order to support interfaces in a more modular way. Please refer to the following rules in the excellent book "C++ Coding Standards" by Herb Sutter and Andrei Alexandrescu:57: Keep a type and its nonmember function interface in the same namespace. 58: Keep types and functions in separate namespaces unless they’re specifically intended to work together.
alexk7
A: 

You're over-using them (and you'll get nothing in return).

Jimmy J
A hierarchical, ordered structure. (re “you’ll get nothing in return”) I think this is huge, and vastly underrated in C++. Just think of how this impacts on documentation.
Konrad Rudolph
@Konrad I completely agree
doc
+1  A: 

Atleast as a small help, in some cases you can do this:

namespace foo = A::B::C::D;

And then reference A::B::C::D as foo. But only in some cases.

sharth
A: 

I agree with Adam. C++ is syntactically most powerful imperative language, but in aspect of modularity this language is a terrible mistake.

And I'm really anxious when I hear C++ fanatics defending even poorest C++ concepts. Adam accurately pointed out that C++ namespaces are unconvenient (and this is not their only sin), so fanatics replied that they are not intended to. The question araises why not? The fanatic won't tell you - they are just flawly designed.

Anyone tried integrate C++ code with Java? Anyone used CORBA? Guess what is generated from IDL, a hierarchical namespaces structure. For WSDL/SOAP large namespace structures are also welocme. And in many other applications.

Go for the battle and tell Java developers that they are all wrong. Tell them that java.lang should be named package std and for example org.eclipse.swt.graphics should be package ibm. Tell them that they shall introduce 5 new keywords for simply including a module and its namespace into program (#ifndef, #define, #include, namespace, using) and veeeeery usefull features like anonymous namespace or aliases and additional grammar. And of course let this commands work against each other. Ehh.

doc
+1  A: 

You can skip indentation. I often write

namespace myLib { namespace details {

/* source code */

}; }; /* myLib::details */

C++ source code is eventually compiled into binary, unlike C#/Java which stays in the binary. Therefore, namespace just provides a fine solution for variable naming conflict. It is not intended for class hierarchy.

I often keep one or two namespace levels in the code.

Sherwood Hu
The semicolons are not needed.
alexk7