2

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.

  • 2
    It is a matter of opinion (so probably off-topic here), but IMHO it is a really bad idea. You don't want deep hierarchies for headers or namespaces in C++ (and this is one of the misfeatures of Java that I hate the most). – Basile Starynkevitch Jul 9 '16 at 14:22
  • 2
    Duplicate of stackoverflow.com/questions/41590/… – SOFe Jul 9 '16 at 14:36
  • 14
    "The idea appears to be popular in the Java world." An excellent reason to never do it. – DeadMG Jul 9 '16 at 14:37
  • 1
    Those who think that this is not an issue should try to use OpenSceneGraph (with the top-level namespace osg) and OpenSG (with the top-level namespace osg) 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.... – Marco13 Jul 11 '16 at 9:41
  • 1
    If you don't like replacing . and - with underscores you could try a sensible mix of "Pascal" case and underscores. e.g. awesome-product.cool-company.com could be transliterated as AwesomeProduct_CoolCompany_com. – Brandin Jul 11 '16 at 13:59
4

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.

In what way is your suggested solution not "built-to-last"?

The thing about namespaces is that it is very easy to move things from one namespace to another. Boost does this all the time. The primary definition of something can live in one namespace, while the name exposed to the user comes from a different one.

If you're just experimenting around to find ways to implement things, then each developer should have their own namespace playground. When you want to evaluate a particular experiment, you use the specific namespace they used. When it comes time to integrate one of the variations into the main project, you can use using declarations to bring those symbols into the project namespace.

Indeed, C++11's inline namespace syntax is particularly useful for this, as it can cause one namespace to effectively live within a containing one. This way, the inner namespace still has all of the ADL rules and so forth as if it lived in the outer one.

All of your developers would develop under the root namespace for your project, but they would each have their own sub-namespaces. When it comes time to select which math library (for example) you want to actually use, you simply declare it inline in your primary namespace. This is also good for versioning; you pick the current version to be inlined, while earlier versions can still be made available without the inline keyword.

That's "built-to-last".

Moreover, it is conceivable that we cooperate with other groups of a similar structure, and then we need something global.

And how would a domain name be in any way different from an arbitrary name you pick? Either someone picked a name unilaterally as your root namespace or you all agreed to work under a specific root namespace. Either way, someone had to pick some sequence of characters.

What does it matter if that sequence of characters is a domain name or not? I don't see how inkscape, for example, would be more likely to be selected by a random person by accident than inskcape.org.

Using a domain name as the agreed-upon namespace name will not make it easier to collaborate. So what purpose does it serve to use a domain name as a namespace name?


1) It is very unlikely that someone will use a domain that is not registered to them as namespace.

For a professional project, maybe. For an indie project where the owner is relying on the facilities of GitHub for their stuff? No. Why use a huge domain name like projname.github.io.com for a namespace? The only useful characters in that name are projname; everything else is pointless noise.

Even for a professional project, is cairo really so likely to conflict with someone that cairographics.org is a better alternative? That's 5 useful characters and 12 characters of noise. And sure, IDEs make it easier to write them thanks to tab completion, but that doesn't make it any easier to read.

Names of such length would encourage someone to do namespace projname = projname.github.io.com;, if not flat out using namespace projname.github.io;. So what do you gain?

That's the difference between C++ and Java. If you do import projname.github.io.com.*, that stays in your single .java file. C++ has headers, which don't belong to a single translation unit. And lots of code gets written in headers.

In C++, you are far more likely to have to use a fully-qualified name for a type than in Java.

2) If it happens that someone uses a domain actually registered to us, then it is pretty obvious that they will have to move, not us.

Why do they "have to move" at all? Because it's registered to you? If they got their first, and their project is more popular than yours, who exactly would expect them to change and break lots of people's code?

The point of namespaces is not to guarantee universal distinction between projects. Otherwise, we'd use UUIDs or SHA hashes or something for root namespaces.

  • 2
    It sounds like your argument is “namespace clashes aren't an issue unless some names clash”, which is a bit empty. Also, Java packages are namespaces and have nothing to do with deployment: a JAR can contain classes from multiple packages. Packages are open, since I can write my own classes for an existing namespace. Packages also carry semantics regarding member visibility. The point of the domain name package naming convention is that we define an authority which is responsible for structuring that part of the package name tree. C++'s lack of such a convention has literally no advantages. – amon Jul 9 '16 at 21:03
  • "It sounds like your argument is “namespace clashes aren't an issue unless some names clash”, which is a bit empty." When was the last time you had a genuine namespace clash in real code? Not a theory or what might happen, but an actual clash? Have people accidentally added things to boost, poco, Qt, Gtkmm or any other namespace from various projects? The domain name convention is a solution looking for a problem. The advantage of not using domain names as a convention is that you're not using domain names. – Nicol Bolas Jul 10 '16 at 15:05
  • 2
    There's no reason to want to use a domain name as a namespace name. – DeadMG Jul 10 '16 at 19:37
  • "Using a domain name as the agreed-upon namespace name will not make it easier to collaborate." It may have 2 advantages. 1) It is very unlikely that someone will use a domain that is not registered to them as namespace. 2) If it happens that someone uses a domain actually registered to us, then it is pretty obvious that they will have to move, not us. – Lasse Kliemann Jul 10 '16 at 22:44
  • @LasseKliemann: I fail to see what either of those things have to do with improving collaboration. I don't see what who has the registration for a domain name has to do with collaborating on a project. What exactly are you concerned with happening if you don't use a domain name? If you ask someone to work with you on a project, then a namespace has to be determined for that project. I don't see any benefit from that namespace being a domain name. – Nicol Bolas Jul 10 '16 at 22:47
0

I'm not a fan of reverse DNS java packaging. There's a few reasons for it.

  • Names of companies change (future IP problem?)
  • Pointless directories like 'com' that never hold anything

When I do C++ development I usually do something like this:

::application::package::subpackage

src/application/package/file.h

#ifndef SRC_APPLICATION_PACKAGE_FILE_H_
#define SRC_APPLICATION_PACKAGE_FILE_H_

namespace application {
namespace package {

class MyClass {
  private:
    ::application::package::OtherClass other_;
};

}  // subpackage namespace
}  // application namespace

#endif

optionally you can do a

using ::application::package::OtherClass;

if you doing a template

A good class example might be something like:

::application::logging::Logger logger_;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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