引用返回值和右值
Reference return and rvalue
我在书中没有这段代码:
template <typename Resource, typename Identifier>
Resource& ResourceHolder<Resource, Identifier>::get(Identifier id)
{
auto found = mResourceMap.find(id);
assert(found != mResourceMap.end());
return *found->second;
}
当变量是一个普通迭代器而不是指针时,为什么我们要取消对它的引用?然后访问它,就好像我们有int obj=new obj()这样的东西&obj->someVar;
此页上的cpp引用http://www.cplusplus.com/reference/iterator/表示可以将迭代器取消引用为右值。
我开始阅读这一页http://thbecker.net/articles/rvalue_references/section_01.html
这是一篇很好的文章,但有点密集,有人可以在我提供的示例代码的上下文中澄清这一点吗?
*found->second
正在取消引用found->second
返回的指针。->
运算符的优先级高于*
(有关完整列表,请参阅运算符优先级),因此该语句实际上与*(found->second)
相同,而不是像您所想的那样与(*found)->second
相同。
相关文章:
- 将值分配给从函数返回的右值引用
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 将返回类型专用化为 void 或 const 左值引用
- 对于具有引用返回类型的搜索算法,默认返回值应该是什么?
- 可以返回右值引用吗?
- 普通的右值引用和 std::forward 返回的引用有什么区别?
- 我可以从取消引用"new"的返回值初始化引用吗
- C++ std::map 的返回值引用
- 按值与右值引用返回
- 为什么我不能将常量左值引用绑定到返回 T&&&的函数?
- 通过引用将函数传递给 TBB 任务时无法返回值
- 了解返回时的右值引用
- 为什么作为返回类型的右值引用不能初始化非常量引用?
- 这安全吗?右值和引用返回值
- 如果函数的返回值将用作右值引用而不是左值,有没有办法使函数具有不同的行为?
- 动态指针引用数组由三元运算符返回值,但有异常
- std::move 返回值 weak_ptr::锁定弄乱了shared_ptr的引用计数?
- 从 r 值引用限定方法返回 r 值引用是否是一种好的做法?
- 返回值(引用、指针和对象)
- 从函数返回值:引用vs结构