Is there any project organization standard for C ++?


In Java and ActionScript3.0 we use namespaces based on the directory path, I see a lot of use of namespace , but are not based on the path of the "location" class in the folder.

I searched a lot if there was any kind of organization that has that "recommendation" for PHP, the PSR-4, but for C ++ I did not find any recommendations, I know it should be trivial, but I think a minimum of organization, size of the team or be a personal project can be a good way, in the case the intention is not to use namespaces in all classes, only in isolated libs that I will reuse for various applications.

Is there something like this for C ++, an "official" design pattern?

If it does not exist, I thought of doing something like this:

  • ./fornecedor/categoria/foo.h

    namespace Fornecedor
        namespace Categoria
            class Foo()
  • ./fornecedor/categoria/foo.cpp

    #include "foo.h"
    using namespace Fornecedor::Categoria;

In this example above:

  • The class name is associated with the file name
  • Files and folders are always in lowercase (lowercase letters)
  • Category would only be to divide the use of classes, for example fornecedor/matematica/soma.cpp and fornecedor/matematica/divisao.cpp

This is just an idea, would it be a good way?

asked by anonymous 20.11.2016 / 01:26

1 answer


C ++ namespace works the same way as in C # . That is, they are only "surnames" for their members, nothing more than that.

One of the advantages of this is that they can be composed. You can put together in the same namespace completely isolated things that you do not know about. So it is a mistake to try to use this mechanism as presented in the question. Not that it can not be done, but it was not meant to be used like that.

It's not that it's going to bring some problem, but it's conceptually wrong, and it may prevent you from doing something in the future by having it organized this way. I would not go that way. It is not officially recommended not to do so, but it is also not recommended to do so.

Note that std works just the way I said it. It consists of several independent parts. It is to have few namespaces , it is not to do like Java. That alias C # did not copy because they considered this form problematic .

What C ++ does differ from C # is that it encourages the naming hierarchy and C ++ does not, should be as simple as possible.

Note that% s of% s is what really determines the organization of what each thing is.

And in the future there will be modules , which maybe be what you want. Depending on how you do it now, you may have difficulty using the new feature.

20.11.2016 / 01:49