【发布时间】:2011-09-27 23:27:43
【问题描述】:
我有一个大量使用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_ptr(boost::shared_ptr 与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_ptr和std::shared_ptr之间的歧义(除了暂时不使用c++0x),以及 - 如果
boost::shared_ptr最终将只是std::shared_ptr的别名;这会自动解决我的问题。
谢谢!
【问题讨论】:
-
解决方案可能是一起摆脱
using namespace std。它可以像对每个 stl 元素进行全局查找和替换一样简单。 -
对,这将是解决方案,但 STL 的使用如此频繁以至于它会使代码的可读性大大降低;也许我可以考虑在公共标头中编写一堆
using std::string; using std::vector;等声明。std::用的部件其实并不多,但是用的很频繁。 -
@eudoxos:需要一些时间来适应,但是代码的易用性增加了很多;另外,我发现自己只是在使用它的范围内添加
using std::cout; using std::string。这增加了可读性,尤其是在不那么琐碎的情况下(using boost::phoenix::bind或using boost:bind或using boost::spirit::karma::_1等。消除了任何歧义(对编译器和程序员而言),而不会弄乱实际代码)
标签: python c++11 boost shared-ptr python-c-api