【发布时间】:2011-07-12 07:26:10
【问题描述】:
我在我的程序中编写了一个路径类来处理层次路径结构。我决定使用 std::shared_ptr 作为整个类的标准返回类型,因为我越来越喜欢它了。
令我惊讶的是,我无法使用 std::copy 或正常的 vector.insert(v.begin(), v.end()) 将元素复制到 shared_ptr 的向量或从这些向量复制。这是为什么呢?
shared_ptr<vector<shared_ptr<bfile>>> butils::bfile::search()
{
shared_ptr<vector<shared_ptr<bfile>>> ret(new vector<shared_ptr<bfile>>());
shared_ptr<vector<shared_ptr<bfile>>> children = getChildren();
//WTF why don't either of these work?
//std::copy(children->begin(), children->end(), back_inserter(ret));
//ret->insert(children->begin(), children->end());
//I've had to resort to doing this....
for (auto c = children->begin(); c != children->end(); c++)
{
ret->push_back(*c);
auto cChildren = (*c)->search();
for (auto cc = cChildren->begin(); cc != cChildren->end(); cc ++)
{
ret->push_back(*cc);
}
}
return ret;
}
当我尝试 std::copy() 时,我得到了:
1>C:\Program Files (x86)\Microsoft 视觉工作室 10.0\VC\include\iterator(21): 错误 C2039: 'const_reference' : 不是 'std::tr1::shared_ptr<_ty>' 的成员 1> 与 1> [ 1>
_Ty=std::vector> 1> ] 1>
BFile.cpp(329):见参考 类模板实例化 'std::back_insert_iterator<_container>' 正在编译 1> 与 1>
[ 1>
_Container=std::tr1::shared_ptr>> 1>]
当我尝试 insert(v.begin(), v.end()) 时,我得到了;
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xmemory(208): error C2664: 'std::tr1::shared_ptr<_Ty>::shared_ptr(std::nullptr_t)' : cannot convert parameter 1 from 'std::_Vector_iterator<_Myvec>' to 'std::nullptr_t'
1> with
1> [
1> _Ty=butils::bfile
1> ]
1> and
1> [
1> _Myvec=std::_Vector_val<std::tr1::shared_ptr<butils::bfile>,std::allocator<std::tr1::shared_ptr<butils::bfile>>>
1> ]
我不确定我是否理解这些编译器错误...还有其他人有线索吗?
【问题讨论】:
-
不要太喜欢
shared_ptr,只要没有“共享”所有权,就更喜欢使用unique_ptr。共享所有权是它自己的一个概念,不应该为了ease而被滥用,因为它有自己的问题(引用循环),只能由内存泄漏工具检测到......经验丰富的程序员。 -
@Matthieu - 我同意你的观点,尽管考虑到这里的对象只是带有字符串和一些状态的路径对象,很难看出它们如何导致引用循环.