Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When have we any practical use for hierarchical namespaces in c++?

Tags:

c++

namespaces

I can understand the use for one level of namespaces. But 3 levels of namespaces. Looks insane. Is there any practical use for that? Or is it just a misconception?

like image 471
FlinkmanSV Avatar asked Dec 10 '22 23:12

FlinkmanSV


2 Answers

Hierarchical namespaces do have a use in that they allow progressively more refined definitions. Certainly a single provider may produce two classes with the same name. Often the first level is occupied by the company name, the second specifies the product, the third (and possibly more) my provide the domain.

There are also other uses of namespace segregation. One popular situation is placing the base classes for a factory pattern in its own namespace and then derived factories in their own namespaces by provider. E.g. System.Data, System.Data.SqlClient and System.Data.OleDbClient.

like image 172
Orion Adrian Avatar answered May 13 '23 04:05

Orion Adrian


Obviously it's a matter of opinion. But it really boils down to organization. For example, I have a project which has a plugin api that has functions/objects which look something like this:

plugins::v1::function

When 2.0 is rolled out they will be put into the v2 sub-namespace. I plan to only deprecate but never remove v1 members which should nicely support backwards compatibility in the future. This is just one example of "sane" usage. I imagine some people will differ, but like I said, it's a matter of opinion.

like image 43
Evan Teran Avatar answered May 13 '23 06:05

Evan Teran