9

我有一个大量使用shared_ptr和 STL 的 c++ 代码。一个常见的标题说

#include<boost/shared_ptr.hpp>
using boost::shared_ptr;  // for shared_ptr
using namespace std;      // for STL

我现在想切换到 c++0x 以利用语言特性,使用 gcc 4.6 和-std=c++0x. 然而std::shared_ptr,现在也有,导致未指定shared_ptrboost::shared_ptrvs std::shared_ptr)的歧义。

改为std::shared_ptr改为时,如下所示:

#include<memory>
using namespace std;      // for STL; also imports std::shared_ptr

然后我遇到了问题boost::python,它boost::shared_ptr仅适用于(至少无需进一步摆弄):

/usr/include/boost/python/object/make_ptr_instance.hpp:30:52: error: no matching function for call to 'get_pointer(const std::shared_ptr<Cell>&)'

因此,我的问题是

  • 如果有一个简单的解决方案来解决和之间的歧义boost::shared_ptrstd::shared_ptr除了现在不使用 c++0x),还有
  • ifboost::shared_ptr最终将只是std::shared_ptr;的别名 这将自动解决我的问题。

谢谢!

4

1 回答 1

3

您需要为共享指针类定义独立函数“get_pointer”,以便它与 Boost Python 一起使用。(请注意,这使您能够编写自己的共享指针,并且仍然可以使用 Boost Python:这是一种有意识的设计工作,以防止不同的 Boost 库紧密耦合)

您可能会使用 boost tr1 兼容性标头来实现这一点,但我还没有尝试过。

http://boost.cowic.de/rc/pdf/tr1.pdf

当 Boost.TR1 被配置为使用您的标准库的本地 TR1 实现时,它不会做太多事情:它只是包含适当的头文件。

当 Boost.TR1 使用特定组件的 Boost 实现时,它会包含适当的 Boost 标头并使用 using 声明在命名空间 std::tr1 中导入必要的声明。请注意,只有那些属于标准的声明才会被导入:实现是故意非常严格地不包括命名空间 std::tr1 中的任何特定于 Boost 的扩展,以便捕获用户代码中的任何可移植性错误。如果您确实需要使用特定于 Boost 的扩展,那么您应该直接包含 Boost 标头并使用命名空间 boost:: 中的声明。请注意,这种实现方式并不完全符合标准,特别是不可能将 TR1 组件的用户定义模板特化添加到命名空间 std::tr1 中。还有一个或两个 Boost 库尚未完全符合标准,任何此类不符合项都记录在 TR1 按主题部分。但是,希望在实践中,非标准行为的发生应该是极其罕见的。

If you use the standard conforming header includes (in boost/tr1/tr1) then these header names can sometimes conflict with existing standard library headers (for example shared_ptr is added to the existing standard library header rather than it's own header). These headers forward on to your existing standard library header in one of two ways: for gcc it uses #include_next, and for other compilers it uses the macro BOOST_TR1_STD_HEADER(header) (defined in boost/tr1/detail/config.hpp) which evaluates to #include <../include/header>. This should work "straight out the box" for most compilers, but does mean that these headers should never be placed inside a directory called "include" that is already in your compiler's search path.

于 2011-07-04T08:27:00.643 回答