Functors and lambda expressions Flashcards

1
Q

What are functors?

A

Functions with state

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why do we want functors?

A
  • Allow more generic code
  • Contains state
  • Can be passed as arguments to stand library containers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are lambda expressions?

A

Anonymous functors, functors that are not explicitly named or identified. Useful for simple cases

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the auto keyword?

A

Gets the compiler to determine the best possible type to use in the context it is given

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When should we use lambda?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When should we not use lambda’s?

A

-Instead of proper function calls.
- When they need to be stateful

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the inline function do?

A

Used for compiler optimisation, substitutes all functors calls to the item in the return statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a stateful functor?

A

Functor with state

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does this line of code imply:
auto plusFive = -> int {}

A

This is a lambda expression that takes an int x and returns an int.
Should be: [] (int x) -> int {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

For lamba expressions how do we capture all the variables of a file?

A

By putting “=” in the rectangular brackets: [=]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Does lambda need to capture the this pointer in classes?

A

No it is automatically captured.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly