Skip to content

Commit

Permalink
Update item3.md
Browse files Browse the repository at this point in the history
update formatting in lvalue-reference-to-non-const and lvalue-references-to-const
  • Loading branch information
yingang authored and y1yang0 committed Mar 1, 2022
1 parent b7bd82f commit 3f18cac
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion 1.DeducingTypes/item3.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ decltype(auto) myWidget2 = cw; //decltype类型推导
template<typename Container, typename Index>
decltype(auto) authAndAccess(Container& c, Index i);
````
容器通过传引用的方式传递非常量左值引用(lvalue-reference-to-non-`const`),因为返回一个引用允许用户可以修改容器。但是这意味着在不能给这个函数传递右值容器,右值不能被绑定到左值引用上(除非这个左值引用是一个const(lvalue-references-to-`const`),但是这里明显不是)。
容器通过传引用的方式传递非常量左值引用(lvalue-reference-to-non-**const**),因为返回一个引用允许用户可以修改容器。但是这意味着在不能给这个函数传递右值容器,右值不能被绑定到左值引用上(除非这个左值引用是一个const(lvalue-references-to-**const**),但是这里明显不是)。
公认的向`authAndAccess`传递一个右值是一个[edge case](https://en.wikipedia.org/wiki/Edge_case)(译注:在极限操作情况下会发生的事情,类似于会发生但是概率较小的事情)。一个右值容器,是一个临时对象,通常会在`authAndAccess`调用结束被销毁,这意味着`authAndAccess`返回的引用将会成为一个悬置的(dangle)引用。但是使用向`authAndAccess`传递一个临时变量也并不是没有意义,有时候用户可能只是想简单的获得临时容器中的一个元素的拷贝,比如这样:
````cpp
Expand Down

0 comments on commit 3f18cac

Please sign in to comment.