Functors and lambda expressions Flashcards
What are functors?
Functions with state
Why do we want functors?
- Allow more generic code
- Contains state
- Can be passed as arguments to stand library containers
What are lambda expressions?
Anonymous functors, functors that are not explicitly named or identified. Useful for simple cases
What does the auto keyword?
Gets the compiler to determine the best possible type to use in the context it is given
When should we use lambda?
- To provide type-specific details to generic implementations of algorithms (e.g. sorting comparators)
- To provide type-safe macros to inline-able equations
- Use them with standard library algorithms
When should we not use lambda’s?
-Instead of proper function calls.
- When they need to be stateful
What does the inline function do?
Used for compiler optimisation, substitutes all functors calls to the item in the return statement.
What is a stateful functor?
Functor with state
This is a lambda expression that takes an int x and returns an int.
Should be: [] (int x) -> int {}
For lamba expressions how do we capture all the variables of a file?
By putting “=” in the rectangular brackets: [=]
Does lambda need to capture the this pointer in classes?
No it is automatically captured.