使用 C++像函数这样的类可以定义在几个不同的位置
在 C++0x、C++03 TR1 和 boost 之间,根据编译器的不同,可以在 3 个不同的位置定义函数和绑定之类的东西,例如,对于 VC pre VC9 功能包,您有 boost 版本,然后您可以将它们定义在 3 个不同的位置,但是在 std::tr1:: 命名空间中,VC10 将其移至 std:: 命名空间。
目前,我现有的代码在 boost:: 命名空间中专门使用旧的 boost 版本,但是对于我的许多应用程序和库来说,我使用的所有 boost 内容现在都在 tr1 和 C++0x 中,如果可能的话,请删除 boost 依赖项从中,同时保留与旧编译器版本的向后兼容性。
但是我不确定如何让我的代码定位、包含然后能够访问正确的版本:(我考虑过的一件事是使用像 _MSC_VER 这样的宏来查看编译器是否包含我想要的类,(回退到 tr1,然后根据需要进行提升),然后使用“using somenamespace::someclass;”内容将有问题的类移动到 std:: 命名空间中。
问题是,在某些情况下,这可能会破坏内容,并且我什至不确定如何判断 VC9 是否安装了功能包或 SP1 :( 我也不确定是否有一种简洁的方法来做到这一点,也许提供我自己的功能性.hpp 来实现所需的“魔法” ?
最主要的是我想开始为新标准编写代码,但在某种程度上它仍然可以在旧编译器上以最小的努力工作。
Between C++0x, C++03 TR1 and boost some things like function and bind can be defined in 3 different places depending on the compiler, eg for VC pre VC9 feature pack you have the boost versions, then you get them in but in the std::tr1:: namespace, and VC10 moves that to just the std:: namespace.
Currently my existing code uses the older boost versions in the boost:: namespace exclusivly, however since for many of my applications and libaries all the boost stuff I used is now in tr1 and C++0x, id like if possible remove the boost dependency from those, while retaining backwards compatibility with older compiler versions.
However I'm not sure on how to make my code locate, include and then be able to access the correct versions :( One thing I have considered is using macros like _MSC_VER to see if the compiler includes the classes I want, (falling back to tr1 and then to boost as needed) and then use the "using somenamespace::someclass;" stuff to move the classes in question into the std:: namespace.
The problem is it seems that in some cases this might break stuff, and I'm not even sure how to tell if VC9 has its feature pack or SP1 installed or not :( I'm also not sure on a tidy way to do it, perhaps provide my own functional.hpp that does the required "magic"?
The main thing is I want to start writing my code for the new standards, but in a way that it will still work with minimal effort on older compilers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Boost。TR1 已经为您做到了这一点 - 它具有编译器/版本检测逻辑,可以使用编译器的 TR1 实现(如果它适用于您的平台),或者使用各种相关的 Boost 库来模拟 TR1(如果不可用):
Boost.TR1 already does this for you – it has compiler/version detection logic to use your compiler's TR1 implementation if it's available for your platform, or use the various relevant Boost libraries to emulate TR1 if not:
从你的帖子来看,你主要关心的是 VC++。如果是这样,那么我认为使用基于 _MSC_VER 的预处理器定义可能是最直接的解决方案。正如您所暗示的,只需找出哪些版本首先提供各种功能并包含适当的标头即可。
至于如何在代码中访问它们,我只会使用几个
typedef
,它们根据包含的标头设置为不同的类型。如果库的 boost、tr1 和 C++0x 版本提供相同(或足够相似)的接口,那么这应该“正常工作”。不过,如果类型是类模板,则普通的 typedef 将不起作用,您必须求助于 #define。有时直截了当也是好的。也许不值得让它变得太复杂。 (如果你想支持的不仅仅是 VC++,你可能必须变得复杂。)
也许是这样的(作为第一个想法):
虽然我不喜欢使用 #define 来获取命名空间,但我不能我突然想到了另一种方法。不过我敢打赌肯定有一个。
From your post it seems like you are primarily concerned about VC++. If thats the case, then I think using preprocessor defines based on the
_MSC_VER
is probably the most straight forward solution. As you hinted at, just find out what versions were first to provide the various features and include the appropriate header.As far as how to access them in your code, I would just use several
typedef
s that are set to different types depending on the included header. If the boost, tr1, and C++0x versions of the libraries provide the same (or similar enough) interfaces, this should "just work". Although, if the type is a class template, plain typedefs won't work, and you'll have to resort to a #define.Sometimes straight forward is good. It may not be worth making it too complicated. (If you want to support more than just VC++, you will probably have to get complicated.)
Perhaps something like this (as a first thought):
Although I don't like using a #define to get the namespace, I can't think of another way off the top of my head. I bet there is one, though.
由于您已经在使用 Boost,请考虑使用 Boost.Config 提供的宏用于测试潜在支持的功能。具体来说,请查看
BOOST_HAS_TR1_*
宏和BOOST_NO_*
宏。请注意,您必须检查各个功能,因为并非所有 TR1 和 C++0x 的实现都支持这些规范的所有功能。
Since you're already using Boost, consider using the macros that Boost.Config provides to test for potentially supported features. Specifically, take a look at the
BOOST_HAS_TR1_*
macros and theBOOST_NO_*
macros.Note that you have to check for individual features, since not all implementations of TR1 and C++0x support all features of those specs.