C++命名空间版本控制
在 C++ 中,可以对命名空间版本控制执行以下操作
第一个版本:
namespace A__v1 {
class X ...
class Y ...
class Z ...
}
namespace A = A__v1
第二个版本(X 类已更改):
namespace A__v1 {
class X ...
class Y ...
class Z ...
}
namespace A__v2 {
class X ...
using A__v1::Y;
using A__v1::Z;
}
namespace A = A__v2
我想知道的是,值得付出努力吗?当更改命名空间的内部结构时,这真的会给您的应用程序/库带来任何优势吗?
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__v1 {
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我实际上喜欢处理这个问题的非宏方法,它允许库的一个构建服务于多个版本。这将不可避免地增加库的大小(由于存在一些类的更多版本),但有一个警告:编译器很可能会报告类的完整命名空间资格,从而使您的库的用户不知道了解您的非标准版本控制方案非常困惑。
再想一想,我也没有看到在一个库构建中提供同一事物的两个版本的用途,除非涉及不同的 CPU/架构,但我不认为这就是您想要的。保留旧版本的类并不明智,如果不需要,人们永远不会切换到新版本,并且如果某些内容(半内部)被弃用,您将删除“库的一部分”的内容“可以这么说。
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.
这是一个很好的技巧并且非常有用,但是有一些问题需要注意。大多数情况下,您不能使用名称空间 A 来专门化模板。
C++0X 有内联命名空间,旨在更好地处理这个问题。
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.