开发者

C++ Namespace versioning

In C++ its possible to do the following for namespace versioning

First version:

namespace A__v1 {
  class X ...
  class Y ...
  class Z ...
}
namespace A = A__v1

Second version (where the X class is changed):

namespace A__v开发者_运维技巧1 {
  class X ...
  class Y ...
  class Z ...
}
namespace A__v2 {
  class X ...
  using A__v1::Y;
  using A__v1::Z;
}
namespace A = A__v2

What i would like to know is, is it worth the effort? Does this really add any advantage to your application/library when changing the internals of a namespace?


I actually like the non-macro way of handling this, it allows one build of the library to serve many versions. This will inevitably increase library size (due to more versions of some classes all being present) but there is one caveat though: the compiler will most likely report the full namespace qualification of the classes, making the user of your library, who doesn't know about your non-standard versioning scheme very confused.

On second thought, I also don't see the use of supplying two versions of the same thing in one library build, except if there are different CPUs/architectures involved, but I don't think that's what you're getting at. Keeping old versions of classes around is not smart, people will never switch to the newer ones if they don't need to, and if something (half-internal) gets deprecated, you'll have removed something that was "part of the library" so to speak.


It's a nice trick and quite useful but there are some problems to be aware of. Mostly, you can't specialize a template using the name A for the namespace.

C++0X have inline namespaces which was designed to handle this better.


the library can be tricky for the programers who will use that namespace, maybe it's better to use separate namespaces independently to make the difference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜