Skip to content

Commit e6032c7

Browse files
authored
Update ITEM33.md
1 parent dd4ad0b commit e6032c7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

ITEM33.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,33 @@ template을 사용했을 때와는 달리 auto의 경우 type parameter ```T```
4242
```c++
4343
auto f = [](auto&& x) { return func(normalize(std::forward<decltype(x)>(x))); };
4444
```
45+
46+
# decltype(x)의 문제점
47+
x의 타입을 여과없이 추론함. 즉, x가 rvalue reference였다면, rvalue reference가 나옴. [참조: decltype](https://github.com/jwvg0425/ModernCppStudy/wiki/item-3)
48+
x가 lvalue reference인 경우는 decltype(x)는 lvalue reference이고, 이것에 대한 것은 성대현 선생님이 [Item28](https://github.com/lunchclass/cpp/blob/master/ITEM28.md)에서 다루었음.
49+
x가 rvalue reference인 경우는 decltype(x)는 rvalue reference이고, 이것은 아무런 문제가 없는가?
50+
51+
std::forward의 구현이 다음과 같을 때,
52+
```c++
53+
template<typename T>
54+
T&& forward<remove_reference_t<T>& param)
55+
{
56+
return static_cast<T&&>(param);
57+
}
58+
```
59+
60+
때려넣으면,
61+
```c++
62+
Widget&& && forward(Widget& param)
63+
{
64+
return static_cast<Widget&& &&>(param);
65+
}
66+
```
67+
68+
하지만 성대현 선생의 [Item28](https://github.com/lunchclass/cpp/blob/master/ITEM28.md)에 따라 reference collapsing되어 결국에는,
69+
```c++
70+
Widget&& forward(Widget& param)
71+
{
72+
return static_cast<Widget&&>(param);
73+
}
74+
```

0 commit comments

Comments
 (0)