I'm working on a library that contains a large number of components that have namespaces that are nested. Also, when a new module inside the library get's added, we then have to add the namespace and sometimes this namespaces can change and then it becomes hellish to maintain.

I have been thinking of a solution or approach to solving this problem and come up with this idea:

The optimal approach would be to have a .h file containing the all of the namespaces:

namespace Config {

     namespace PCA = LibraryName::Statistical::Components::PCA; 

And then I can include this config file into the main entry to the application..

But, how I get to this point is what I am having trouble getting to. So here is my thought process:

If I create a "pre-compiled" Config.h that contains something like (Syntax is not right, but I would parse this)

namespace Config {

     "aliases" = [

         "PCA" =>  "LibraryName::Statistical::Components::PCA"

Then, some way inside the Makefile parse this syntax, and generate a .h file, with the aliases inside it and then this would work.. But I don't know if this is possible with a Makefile?

The second approach, would be to create a shell command, on the lines of: build_config which then parses this config file and then generate the .h to be used in the entry to the program?

Alternatively, I can use JSON and then parse the JSON using any of the above..

Can anyone give me any guidance to which approach would be better for this, also, is this just causing unnecessary problems?

ALSO: A series of clients / Users have also asked for the ability to change the namespace aliases..

  • At first glance I'm having a hard time seeing how adding more namespaces and aliases will solve the problem of having too many namespaces. On second glance, maybe what you need is to have a stable "public" namespace hierarchy separate from the unstable one you use to organize your implementation?
    – Ixrec
    Aug 26, 2015 at 20:27
  • @lxrec - Thanks for the reply. I'm not saying that having too many namespaces, I'm more getting to the point where, if I want to to add an aliases then I can do this in the config, rebuild the config file without having to re-build the entire library each time.. If I just need to change an aliase, or I have made a simple mistake at naming one, I can easily correct it.. (github.com/rappasoft/laravel-5-boilerplate/blob/master/config/…) here is an example (Although written in PHP) they allow for aliases to be used in a config which is registered when the application is launched..
    – Phorce
    Aug 26, 2015 at 20:34
  • Are these compile-time aliases or run-time aliases? Aug 27, 2015 at 5:32
  • @RobertHarvey They will be runtime aliases, I'm thinking that I can just create a .h file, which has a function "aliases" which runs in the main.cpp file and contains all of the aliases
    – Phorce
    Aug 27, 2015 at 6:15

1 Answer 1


This is a classic XY problem. You have problem X, which is causing problem Y, so you're asking for the solution to problem Y. But the best way of solving problem Y is to solve problem X.

You're asking about generating namespace alias configuration files (Y). But your real problem (X) is that your namespacing is way too complex! C++'s namespaces are not intended to be deeply nested or particularly long. They're intended to be short and not deeply nested. Generally a single namespace is all that is required for a small to medium-sized library, and even large libraries if well-designed can sit in a single namespace.

Nearly all of the C++ standard library, not a small library at all, is in std. There's also std::chrono and a couple of other specialised namespaces, but we don't use std::containers::vector or std::algorithms::sorting::stable_sort, we use std::vector and std::stable_sort.

Simplify the namespacing and you won't need to generate a namespace alias configuration file or anything silly like that.

  • 2
    There are also lots of inline-namespaces for versioning and such, but one can mostly ignore their existence. Sep 7, 2015 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.