Archived

This topic is now archived and is closed to further replies.

nested namespaces in C++

This topic is 5320 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

i am prototyping a large project, and i am using nested namespaces to categorize a lot of the stuff:
namespace ProjectName
{
namespace SubNamespace
{
...
};
namespace OtherSubNamespace
{
...
};
}; 
i have to type out the two "namespace" lines, and also the two closing brackets. is there a "shortcut" way of doing this? i tried:
namespace ProjectName::SubNamespace
{
...
};

but that doesn''t work. it isn''t too important; i am curious mainly.

Share on other sites
I don''t know of any "real" quick ways of doing it, but you can always cheat using macros:
#define NAMESPACE2_BEGIN(outer, inner) \  namespace outer { \    namespace inner {#define NAMESPACE2_END() \    } \  }// To use them:NAMESPACE2_BEGIN(ProjectName, SubNamespace)  // ...NAMESPACE2_END()

Or something like that.

I just write out the namespaces and just neglect to tab them out for large sections (the files I do that to have everything in a single namespace, so it''s not too confusing to read without the tabbing), it''s really not that bad . I think the only C++ project I''m working on has namespaces nested as deeply as 5 levels in parts (most of that stuff is just implementations of interfaces though)...

Share on other sites
thanks for the tip, although i don''t think it is worth that much effort (i am blocking things together in separate files anyway, so it only happens once per source file).

Share on other sites
It''s done like this:
namespace alias = ProjectName::SubNamespace;

1. 1
2. 2
JoeJ
14
3. 3
4. 4
5. 5
frob
11

• 13
• 16
• 13
• 20
• 12
• Forum Statistics

• Total Topics
632178
• Total Posts
3004604

×