Update item3.md

This commit is contained in:
猫耳堀川雷鼓 2021-03-02 11:39:14 +08:00 committed by GitHub
parent 4753623c99
commit 55885ba78a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@
我们将从一个简单的情况开始,没有任何令人惊讶的情况。相比模板类型推导和`auto`类型推导(参见[Item1](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/1.DeducingTypes/item1.md)和[Item2](https://github.com/kelthuzadx/EffectiveModernCppChinese/blob/master/1.DeducingTypes/item2.md)`decltype`只是简单的返回名字或者表达式的类型:
````cpp
const int i=0; //decltype(i)是const int
const int i = 0; //decltype(i)是const int
bool f(const Widget& w); //decltype(w)是const Widget&
//decltype(f)是bool(const Widget&)
@ -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&
}
````