Reference: https://github.com/changkun/modern-cpp-tutorial
Lambda expression
Basic Syntax:
[capture list] (parameter list) mutable(optional) exception attribute -> return type {
// Function body
}
1 | // Example |
- capture list: a type parameter -> can serve to transfer external data to lambda expression.
- Value Capture: Similar to parameter passing by value - Generated when it is created not when called.
- value = 1, lambda (value), value = 100, lambda call -> lambda value = 1
- Reference Capture: parameter passing by refernce
- lambda value = 100
- Implicit capture: Writing capture list is tired, so compiler supports many implicit ways
- [] - empty capture list
- [name1, name2] - captures series of variables
- [&] - reference capture: compiler will decide which one to refer itself.
- [=] - value capture: compiler execute the list of derivation applications itself.
- Expression Capture: Above value and reference caputre capture lvalue (variable name) as it captures the ones that are outer scope. This capture allows captured members to be initialized with arbitrary expressions; rvalues. Type is being judged like auto.
- Value Capture: Similar to parameter passing by value - Generated when it is created not when called.
1 | // expression capture |
Generic Lambda
- We learned auto cannot be used in the parameter list as it would conflict with the functionality of the template.
- Lambda expression is not templated -> the parameter table cannot be generalized, hence the parameter table type must be clarified -> From C++14 you can use auto.
1 | // From C++14, you can use auto keyword to generate generic meanings. |
Function Object Wrapper
std::function - generic, polymorphic function wrapper whose instances can store, copy, and call any target entity that can be called; a container of functions. -> We can more easily handle functions and function pointers as objects.
std::bind - solves the requirement that we may not always be able to get all the parameters of a function at one time.
std::placeholder::_N - Indicate nth element is not given yet, so when the parameters are given later, it fills from the front.
rvalue Reference
It solves a large number of historical issues in C++; extra overheads for vector,string, and allows the function object container, std::function possible.
- lvalue: is a persistent object that still exists after an expression (not necessarily assignment expression). 주소값을 받을 수 있는 모든 것.
- rvalue: is the temporary object that no longer exists after the expression ends. 참조 할 수 없는 값.
- move(a) 는 a를 rvalue로 바꾸어서 lvalue 로 이동하는것; unconditionally conver lvalue -> rvalue
- pvalue (pure): purely rvalue/literal like 10, true. Temporary variables returned by non-references, temp variables generated by operation expressions, origin literals, lambda expressions.
- xvalue (expiring): A value that is destroyed but can be moved. -> T&&
1 | void reference(std::string& str){ |
Move Semantics
1 |
|
Above do not use copy constructs -> enhance performance
Perfect Forwarding
No matter what type of reference type the template parameter is, the template parameter can bederived as right reference type if and only if the argument type is a right reference.
1 | void reference(std::string& str){ |
reference contraction rule