What would be a good system to use domain names as C++ namespaces in order to avoid name clashes between developers? The idea appears to be popular in the Java world.
We cannot use namespace example.com { ... }
, since dots are not allowed.
A practical, but as I find not very elegant, workaround would be to replace each dot by a character that usually is not part of any domain, such as an underscore (I read somewhere that domains may theoretically also contain underscored, but I do not see this is widely used). That would make it: namespace example_com { ... }
.
We could use nested namespaces, thereby effectively replacing the dot by the namespace separator ::
, so that would be: namespace example { namespace com { ... }}
and we would refer to it by example::com
. The drawback of this is that it suggests that com
is a part of example
. But I would like to treat the domain as one unit. Otherwise, reverse domain notation would be fine: com::example
. But my impression is that the relaxed way that domains can be registered has made this hierarchy meaningless for the most part. What would namespace com
(the outer namespace) stand for? Answer: for code written by anyone with a .com
domain, and that can be almost anyone. What would namespaces net
or info
stand for? It does not look useful to me.
Anyone got a better idea or can rebut my critique regarding the meaning of top level domains?
Update: here is some background to my question. I am supervising a small team of developers. In some part, they work on libraries with competing functionality (this is intended, since we are experimenting with different ways to do things). So we had name clashes already (imagine libraries called libmatrix
or even libmisc
). The easy solution would be one namespace per developer, and I keep track of which names are used. However, I would prefer something more built-to-last. Moreover, it is conceivable that we cooperate with other groups of a similar structure, and then we need something global.
osg
) and OpenSG (with the top-level namespaceosg
) at the same time. This is an issue, and it is solved nicely in Java. For C++, one could consider using the domain name without the TLD part, but as more TLDs pop up, collisions become more likely....awesome-product.cool-company.com
could be transliterated asAwesomeProduct_CoolCompany_com
.