[英]Is object std::shared_ptr findable by its std::weak_ptr?
所以我有一个std::vector<std::shared_ptr<T>> myListOfT;
我有一个std::weak_ptr<T> ptrToOneT;
这是从用于填充该容器的指针之一创建的(假设我在回调函数中有它)。 请问std::find
在那个容器上,我的weak_ptr
给我一个原始shared_ptr
的迭代器(如果这个存在于集合中)? 它是否在标准的某个地方得到保证还是依赖于此实现?
我们可以逃脱不锁定weak_ptr
使用std::weak_ptr::owner_before
。 我将使用比必要更详细的解决方案,并引入owner_equal
,它与std::owner_less
相对应:
template<typename T>
class owner_equal
{
private:
template<typename L, typename R>
static bool e(L const& l, R const& r)
{ return !(l.owner_before(r)) && !(r.owner_before(l)); }
public:
using S = std::shared_ptr<T>;
using W = std::weak_ptr<T>;
bool operator()(S const& l, W const& r) const { return e(l, r); }
bool operator()(W const& l, S const& r) const { return e(l, r); }
};
使用此函数对象类型,我们可以自定义std::find_if
:
using T = int;
std::vector<std::shared_ptr<T>> myListOfT =
{std::make_shared<int>(0), std::make_shared<int>(1), std::make_shared<int>(2)};
int const pos = 1;
std::weak_ptr<T> ptrToOneT = myListOfT[pos];
auto pred = [&ptrToOneT](std::shared_ptr<T> const& e)
{ return owner_equal<T>{}(e, ptrToOneT); };
auto const r = std::find_if(begin(myListOfT), end(myListOfT), pred);
assert(r - begin(myListOfT) == pos);
lambda可以用bind-expression替换,例如:
auto pred = std::bind(owner_equal<T>{}, std::cref(ptrToOneT),
std::placeholders::_1);
@davidhigh建议优化:
template<typename FwdIt, typename T>
FwdIt findWeakPtr(FwdIt b, FwdIt e, std::weak_ptr<T> const& w)
{
if(w.expired()) return e;
else
{
auto pred = [&w](std::shared_ptr<T> const& e)
{ return owner_equal<T>{}(e, w); };
return std::find_if(b, e, pred);
}
}
(未测试)
这也稍微改变了行为:如果weak_ptr
是“空”,例如,已经从空的shared_ptr
或通过默认的ctor创建,它将通过owner_equal
比较任何空的shared_ptr
。 但是,在这种情况下, weak_ptr::expired
为true。 因此,优化版本将不会在该范围内找到空的共享指针。
应该在范围内找到空的共享指针吗?
考虑:
using T = int;
std::vector<std::shared_ptr<T>> myListOfT =
{std::shared_ptr<T>(), std::shared_ptr<T>()};
int const pos = 1;
std::weak_ptr<T> ptrToOneT = myListOfT[pos];
auto const r = my_weak_ptr_find(begin(myListOfT), end(myListOfT), ptrToOneT);
auto const r_pos = r - begin(myListOfT);
空共享指针是相等的 。 因此,如果您允许查找空的共享指针,则可以使用r_pos != pos && r != end(myListOfT)
。 例如,此答案中的第一个算法版本产生r_pos == 0
。
有关其他上下文,请参阅:
std::weak_ptr::lock()
是你将weak_ptr
“提升”为shared_ptr
:
std::weak_ptr<T> ptrToOneT;
auto observe = ptrToOneT.lock();
if (observe) {
// observe now shares ownership of the one T
}
else {
// there is no managed object or it has already
// been destroyed
}
如果lock()
成功,那么你可以使用正常的std::shared_ptr<T>
来find()
就像容器中的任何其他对象一样。 虽然你可能不需要find()
它,因为你已经拥有它(除非你想erase()
它或什么)。
侧注,使用shared_ptr
,引用“原始shared_ptr
”并没有多大意义。
我几天前在自己的代码中遇到过类似的问题。 根据我的SO研究,你可以做你想要的。 锁定弱指针,如果共享指针未过期,则使用std::find
struct A{};
int main()
{
std::vector<std::shared_ptr<A> > sptr_vec;
std::weak_ptr<A> wptr;
if(auto sptr = wptr.lock())
{
auto it = std::find(std::begin(sptr_vec), std::end(sptr_vec), sptr);
if (it != std::end(sptr_vec))
{
std::cout<<"found"<<std::endl;
}
}
}
请注意,C ++标准本身并不重要 - 共享指针比较的核心是所包含的原始指针的比较,即比较内存中的地址。
或者,如果你有一个弱指针向量,你可以使用std::find_if
和一个动态锁定的谓词:
std::vector<std::weak_ptr<A> > wptr_vec;
std::shared_ptr<A> sptr;
auto it = std::find_if(std::begin(wptr_vec), std::end(wptr_vec)
, [&sptr](auto const& w){ auto s = w.lock();
if (s) {return s == sptr;}
return false; });
if (it != std::end(wptr_vec))
{
std::cout<<"found"<<std::endl;
}
请注意,在这个应用程序中,我会考虑nullptr
与其自身的等价性,即nullptr == nullptr
为true
,这是不需要的。 因此,我从谓词(以及第一个代码块中的搜索)中排除了这种情况。
编辑:刚刚考虑owner_lock
解决方案,如果只是搜索,这是有利的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.