Could I create a global namespace hierarchy in C++ that is similar to C#'s to assist developers using our code?

StackOverflow https://stackoverflow.com/questions/21287313

  •  01-10-2022
  •  | 
  •  

Question

Is it possible to create a namespace hierarchy in C++ that resembles how it works in C#. For instance if I were to need a type to deal colors within C#, I could dive down through the namespaces to get the appropriate type by using the:

System.drawing.color;

type. In this case, it appears the namespaces are resolved at a project level and determined by the namespaces that the type is contained within. For the developer that this code targets, they get all of this in their auto-complete and aren't left searching through folders and files for that type. In C++ I might have to first include the appropriate header file, at which point I feel like we've already gone through the trouble of grepping source code for the appropriate types and finding which file includes those types. In C++ it would look like:

#include "Core/Color.h"
Color c = new Color();

The IDE offers me little help here. I have to do a directory search for the correct type. The directory paradigm seems to break down unless everyone specifically uses the right filenames and directory structure, which isn't always the case.

To fix this, it looks like not only would I have to come up with a namespace hierarchy for all of my types, which isn't such a large problem, but I'd have to also come up with a header hierarchy to eliminate the problem of constantly grepping the code to find the correct files that include those types.

Does a master header hierarchy present a problem for the compiler, preprocessor, or resulting compiled code since we'd essentially have every other header up the chain (up to a point of course) included in new files?

In the end I want a way to assist the developers who use this code by giving their IDEs a way to dive down to all the types without having to do all of the grepping that we currently have to do. There may be a way to quickly do this within IDEs already, at which point I wouldn't need to utilize the language itself to solve this sort of development problem, however I can't seem to find it.

Was it helpful?

Solution

See the following SO discussion and how this was handled by one of the SO users C++ namespaces advice http://www.adamjamesnaylor.com/2012/10/23/NestedNamespacesInC.aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top