本文介绍了如何处理不断变化的C ++的std ::命名空间?例如:性病:: TR1的:: shared_ptr的主场迎战的std :: shared_ptr的主场迎战的boost :: shared_ptr的主场迎战的boost :: TR1的:: shared_ptr的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于code我目前的工作,我们有时需要编译与旧的编译器的一些较老的系统(EG-我们运行的是较旧的IBM蓝色基因/ L的模拟人生,谁的支持合同规定一些比较旧的C ++编译器)。在code本身利用shared_ptrs,而最初写入使用std :: TR1的:: shared_ptr的。当旧的蓝色基因机器上编译,我很快就意识到,它没有一个TR1 ::实现,所以我切换到升压:: shared_ptr的。原来也有一个boost :: TR1的:: shared_ptr的。现在,code正在被更广泛地使用我们的研究组之外,便携性变得更加重要。

For the code I am currently working on, we sometimes need to compile on some older systems with older compilers (e.g.- we run sims on an older IBM BlueGene/L, who's support contract dictates some quite old C++ compiler). The code itself makes use of shared_ptrs, and was originally written to use std::tr1::shared_ptr. When compiling on the old BlueGene machine, I quickly realized that it doesn't have a tr1:: implementation, and so I switched to boost::shared_ptr. Turns out there is also a boost::tr1::shared_ptr. Now that the code is being used more widely outside of our research group, portability is becoming even more important.

什么是(的?)最好的处理这些类型的肥胖型codeBase的演进标准库问题的做法?我假设,在新的C ++标准11,shared_ptr的将不再是在TR1命名空间,增加了另一个潜在:性病:: shared_ptr的,但我猜wides $ P $垫支持,这将是一个办法关闭。我想使用最新标准,如果可能的,但需要保持便携性。如果我只是坚持提升?

What is a (the?) best practice for handling these sorts of evolving standard library issues in a large-ish codebase? I am assuming that in the new C++11 standard, shared_ptr will no longer be in the tr1 namespace, which adds another potential: std::shared_ptr, however I am guessing widespread support for this will be a ways off. I'd like to be using the latest standard if possible, but need to maintain portability. Should I just stick with boost?

推荐答案

要检测哪些命名空间中的shared_ptr的是,你需要像autoconf的 - 这就是为什么autoconf的创建(检测平台/编译器的变化)的原因。你可以这样做:

To detect which namespace the shared_ptr is in, you need something like autoconf -- this is the reason why autoconf was created (detecting platform/compiler variations). You can do this with:

AC_LANG(C++)

AC_MSG_CHECKING([for std::shared_ptr])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
    [[#include <memory>]]
    [[std::shared_ptr<int> have_shared_ptr;]])
], [
    AC_MSG_RESULT([yes])
    AC_DEFINE_UNQUOTED([HAVE_STD_SHARED_PTR], 1, [Define to 1 if you have the `std::shared_ptr' class.])
], [
    AC_MSG_RESULT([no])
    AC_DEFINE_UNQUOTED([HAVE_STD_SHARED_PTR], 0, [Define to 1 if you have the `std::shared_ptr' class.])
])

重复的std :: TR1的:: shared_ptr的的boost :: TR1的:: shared_ptr的的boost :: shared_ptr的

您可以再创建一个 shared_ptr.hpp 文件,该文件是这样的:

You can then create a shared_ptr.hpp file that is something like:

#include <config.h>

#if defined(HAVE_STD_SHARED_PTR)
    namespace ptr = std;
#elif defined(HAVE_STD_TR1_SHARED_PTR)
    namespace ptr = std::tr1;
#elif defined(HAVE_BOOST_SHARED_PTR)
    namespace ptr = boost;
#elif defined(HAVE_BOOST_TR1_SHARED_PTR)
    namespace ptr = boost::tr1;
#else
#   error No shared_ptr found.
#endif

...然后你就可以使用如:

... which you can then use as:

ptr::shared_ptr<int> pointer(new int(5));

这篇关于如何处理不断变化的C ++的std ::命名空间?例如:性病:: TR1的:: shared_ptr的主场迎战的std :: shared_ptr的主场迎战的boost :: shared_ptr的主场迎战的boost :: TR1的:: shared_ptr的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 22:25