I'm working开发者_如何转开发 on a project where a mixture of C# (95%) and C++/CLI (5%) are used.
The namespace naming convention I'm aiming for is the good old Company.Technology.Etc.
. This works perfectly fine for C#. Now, can I carry this across to C++ classes? I read here that compound namespaces aren't supported in C++.
Am I stuck with the clumsy
namespace Company {
namespace Technology {
namespace Etc {
...
}
}
}
in order to stay consistent?
Is it worth trying to stay consistent?
Yes, you're stuck with it, see this link. In my opinion it's worth using it as it is more readable and manageable to use namespaces. I normally do so in native C++ also.
Starting with C++17 you will be able to use this form:
namespace Company::Technology::Etc {
// ...
}
Which is equivalent to your example:
namespace Company { namespace Technology { namespace Etc {
// ...
}}}
Arg! We did that here for some code too. I hate that.
Is there seriously any confusion there about who signs your paychecks? What happens when Company
gets bought by OtherCorp
, or the marketing department decides that Technology
will henceforth be known as eTech
? How much code will have to be changed?
Generally, I'm anti-use. But in situations like this, where the namespaces should probably never have been made in the first place and you are just saving yourself future code changes, I encourage the following line instead of sticking that huge wart in front of every reference:
using Company::Technology;
Namespaces should generally be created when you have several related classes and objects that you are tempted to stick the same name in their identifier. That way, instead of referring to them as foo_operation
and foo_class
you do foo::operation
and foo::class
.
To define types in the nested namespace, yes you have to do it as you describe.
But to use the types, try
namespace CTE = Company::Technology::Etc;
, then you should be able to do eg CTE::SomeClass someClass;
The accepted answer is in visual studio 2015 no longer true.
You can use with all sidestories the compilerflag /std:c++latest and get the nested namespaces.
精彩评论