mirror of
https://github.com/CnTransGroup/EffectiveModernCppChinese.git
synced 2025-01-27 12:21:01 +08:00
Update item3.md
This commit is contained in:
parent
4753623c99
commit
55885ba78a
@ -17,19 +17,19 @@ struct Point{
|
||||
|
||||
Widget w; //decltype(w)是Widget
|
||||
|
||||
if (f(w))... //decltype(f(w))是bool
|
||||
if (f(w))… //decltype(f(w))是bool
|
||||
|
||||
template<typename T> //std::vector的简化版本
|
||||
class vector{
|
||||
public:
|
||||
...
|
||||
…
|
||||
T& operator[](std::size_t index);
|
||||
...
|
||||
}
|
||||
…
|
||||
};
|
||||
|
||||
vector<int> v; //decltype(v)是vector<int>
|
||||
...
|
||||
if (v[0] == 0)... //decltype(v[0])是int&
|
||||
…
|
||||
if (v[0] == 0)… //decltype(v[0])是int&
|
||||
````
|
||||
看见了吧?没有任何奇怪的东西。
|
||||
|
||||
@ -65,7 +65,7 @@ auto authAndAccess(Container& c, Index i) //不那么正确
|
||||
|
||||
````cpp
|
||||
std::deque<int> d;
|
||||
...
|
||||
…
|
||||
authAndAccess(d, 5) = 10; //认证用户,返回d[5],
|
||||
//然后把10赋值给它
|
||||
//无法通过编译器!
|
||||
@ -159,13 +159,13 @@ int x = 0;
|
||||
decltype(auto) f1()
|
||||
{
|
||||
int x = 0;
|
||||
...
|
||||
…
|
||||
return x; //decltype(x)是int,所以f1返回int
|
||||
}
|
||||
|
||||
decltype(auto) f2()
|
||||
{
|
||||
int x =0l;
|
||||
int x = 0;
|
||||
return (x); //decltype((x))是int&,所以f2返回int&
|
||||
}
|
||||
````
|
||||
|
Loading…
Reference in New Issue
Block a user