Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea4
Effective Modern C++ StudyC++ Korea5
6
Effective Modern C++ StudyC++ Korea7
Effective Modern C++ StudyC++ Korea8
Effective Modern C++ StudyC++ Korea9
By-value capture 사용시 문제점 : raw-pointer를 copy해서 lambda에서 쓰는 경우, lambda식 밖에서 포인터가 delet될 수 있음.
Effective Modern C++ StudyC++ Korea10
Effective Modern C++ StudyC++ Korea11
Effective Modern C++ StudyC++ Korea12
Effective Modern C++ StudyC++ Korea13
Effective Modern C++ StudyC++ Korea14
Effective Modern C++ StudyC++ Korea
Effective Modern C++ StudyC++ Korea17
Effective Modern C++ StudyC++ Korea18
Effective Modern C++ StudyC++ Korea19
Effective Modern C++ StudyC++ Korea20
Effective Modern C++ StudyC++ Korea22
auto GL = [](auto ) { return F( ); };
class CompilerGeneratedClosureClass{
public:
template <typename T>
auto operator() (T X) const
{
return F(X);
}
};
Compiler는 operator() 를 가진 template closure class로 구현
Effective Modern C++ StudyC++ Korea23
auto GL = [](auto&& ) { return F(std::forward<???>(X)); };
만약 F(X)가 R-Value와 L-Value를 다르게 취급해야 한다면 ???
Perfect-forward(X)를 전달해야 한다.
1. X가 Universal Reference 여야 한다.
2. std::forward를 거쳐서 F( ) 에 전달되어야 한다.
auto GL = [](auto ) { return F( ); };
아놔 근데… ??? 에 뭘 적어야 하나 ???
template closure class 에는 T가 있지만, Lambda에는… @#$%?
template <typename T>auto operator() (T X) const{
return F(X);}
Effective Modern C++ StudyC++ Korea24
Universal Reference
L-Value
R-Value
L-Value Reference
R-Value Reference
decltype(x)
L-Value
R-Value
L-Value Reference
R-Value Reference
std::forward<T>
L-Value Reference
Non-Reference
L-Value
R-Value
Effective Modern C++ StudyC++ Korea25
template<typename T>T&& forward(remove_reference_t<T>& param)
{
return static_cast<T&&>(param);
}
Instantiation of std::forward when T is Widget
Widget&& forward(Widget& param)
{
return static_cast<Widget&&>(param);
}
R-Value 타입의 Widget을 Perfect-forward하기를 바랄 때,
T를 Non-Reference로 하는 규칙 대신에, R-Value Reference로 지정하는 것을 고려해 본다면…
한마디로 T가 Widget이 아니라 Widget&& 일 경우를 한번 보자는 거다.
Effective Modern C++ StudyC++ Korea26
Widget&& && forward(Widget& ){
return static_cast<Widget&& &&>(param);
}
&& && 가 도대체 뭥미 ? Reference-Collapsing (참조 붕괴) 적용
Widget&& forward(Widget& param)
{
return static_cast<Widget&&>(param);
}
어라 ?
T를 R-Value Reference로 Instantiation 한거랑,
Non-Reference 로 Instantiation 한거랑 같다.
에헤라 디야~ 옳다구나.
Effective Modern C++ StudyC++ Korea27
decltype(x)
L-Value
R-Value
L-Value Reference
R-Value Reference
std::forward<decltype(x)>
Non-Reference
L-Value
R-Value
auto PFL = [](auto&& ) { return F(std::forward<decltype(x)>(X)); };
Effective Modern C++ StudyC++ Korea28
Any number of parameters
auto PFL = [](auto&& ) { return F(std::forward<decltype(X)>(X)); };
auto PFL = [](auto&&… param) { return F(std::forward<decltype(param)>((param)…)); };
Effective Modern C++ StudyC++ Korea29
• std::forward<T>(X)에 auto&& 를 넣을 땐 decltype 을 사용하자.
• 즉, std::forward<decltype(X)>(X) 이렇게 쓰라는 거다.
http://devluna.blogspot.kr/2015/03/item-33-stdforward-auto-decltype.html