Mar*_*rkD 9 c++ portability boost tr1 std
对于我目前正在处理的代码,我们有时需要使用较旧的编译器在一些较旧的系统上进行编译(例如,我们在较旧的IBM BlueGene/L上运行sims,谁的支持合同规定了一些相当旧的C++编译器).代码本身使用shared_ptrs,最初编写使用std :: tr1 :: shared_ptr.在旧的BlueGene机器上编译时,我很快意识到它没有tr1 :: implementation,所以我切换到boost :: shared_ptr.结果还有一个boost :: tr1 :: shared_ptr.既然代码在我们的研究小组之外被广泛使用,便携性变得更加重要.
在大型代码库中处理这些不断发展的标准库问题的最佳实践是什么?我假设在新的C++ 11标准中,shared_ptr将不再位于tr1命名空间中,这增加了另一个潜力:std :: shared_ptr,但我猜这种广泛的支持将是一个方法.如果可能的话,我想使用最新的标准,但需要保持可移植性.我应该坚持提升吗?
要检测shared_ptr所在的命名空间,需要像autoconf这样的东西 - 这就是创建autoconf的原因(检测平台/编译器变体).你可以这样做:
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.])
])
Run Code Online (Sandbox Code Playgroud)
重复std::tr1::shared_ptr,boost::tr1::shared_ptr和boost::shared_ptr.
然后,您可以创建一个shared_ptr.hpp类似于的文件:
#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
Run Code Online (Sandbox Code Playgroud)
...然后你可以用作:
ptr::shared_ptr<int> pointer(new int(5));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3132 次 |
| 最近记录: |