Functors() - Function objects - functions with state | Modern Cpp Series Ep. 99

  Рет қаралды 13,998

Mike Shah

Mike Shah

Күн бұрын

Пікірлер: 37
@kenk4552
@kenk4552 6 ай бұрын
Your modern Cpp series is a hidden gem. Will for sure get more views!!
@MikeShah
@MikeShah 6 ай бұрын
Cheers!
@buddyairguy2249
@buddyairguy2249 10 ай бұрын
As usual -> Great lesson!
@MikeShah
@MikeShah 10 ай бұрын
Cheers!
@bsdooby
@bsdooby 2 жыл бұрын
"A functor is an instance of type T whose call operator () is overloaded". This implies state.
@mamariomiamo5596
@mamariomiamo5596 2 жыл бұрын
Hi Mike, thank you for another great video! I do have one question: when you mention "we can take advantage of move assignment" at 10:15 by using the initializer list for the constructor, what do you mean exactly?
@rhdi86
@rhdi86 2 жыл бұрын
I think in the sense that we construct the attribute mHealth (by assigning it the value h) and the object Goblin at the same time. Instead of a possible copy would occur if an object is already present in the memory Let's see what coach Mike will respond 😀
@Southpaw101
@Southpaw101 2 жыл бұрын
I have the same question
@MikeShah
@MikeShah 2 жыл бұрын
​@@Southpaw101 Hi team, that's correct. We avoid a copy with initializer list (kzbin.info/www/bejne/rJuke2d7iL1kgMU) and then use something like: m_health(std::move(health)) and avoid making extra copies.
@alexanderwhillas6431
@alexanderwhillas6431 2 жыл бұрын
I guess this is not the same as functors from category theory, where functors are a mapping from one category to another while preserving structure. So for example transforming one container into another. This feels more like a monad in which you wrap some object in another.
@MikeShah
@MikeShah 2 жыл бұрын
Indeed, not as strict as the category theory definition what I'm showing here, but a monad would be more related. :)
@LeflxLight
@LeflxLight 9 ай бұрын
Thank you very much for the video , Its really amazing !!! These videos really help me clear my concepts ! Everything is clearly or smoothly explained with perfect pace. Loved it😀😀
@MikeShah
@MikeShah 9 ай бұрын
Cheers!
@papertowers4216
@papertowers4216 2 жыл бұрын
give the goblin his gold
@MikeShah
@MikeShah 2 жыл бұрын
😁
@kirandhegaskar9298
@kirandhegaskar9298 10 ай бұрын
Hi mike in sort function third parameter we are calling goblincomparator and then our functor gets called ,but is this right way to called functor ,because whenever we write goblincomparator() it should called constructor not functor
@michapawlik6501
@michapawlik6501 9 ай бұрын
You literally solved the problem I had with writing a better version of std::vector that would filter data. 3 lines of code and couple hours of work spared
@MikeShah
@MikeShah 9 ай бұрын
Cheers, happy to hear that!
@peufipeufhhh2405
@peufipeufhhh2405 3 ай бұрын
Where can we get those slides?
@sumitpatil1955
@sumitpatil1955 Жыл бұрын
I want to use one function from other cpp file in other cpp file without include. I mean I want to use functor here. The functor will be a callable function and I am going to use it in curl. Do you have any idea?
@MikeShah
@MikeShah Жыл бұрын
Hmm, you need at the least a forward declaration for that function (e.g. something like myclass::operator()).
@PaulFromMalta
@PaulFromMalta Жыл бұрын
Thanks for the vid! Is the comparator callable object more like a general function since it’s not storing any state? You could swap it out with a lambda and have the same effect right?
@MikeShah
@MikeShah Жыл бұрын
For conparator, could use general function if no state needed, can also use things like std::less or lambda.
@xbz24
@xbz24 10 ай бұрын
Cheers
@MikeShah
@MikeShah 10 ай бұрын
🥂
@smarthumanism3221
@smarthumanism3221 2 жыл бұрын
Than you for the excellent teaching. If I may, I'd like to know how this works internally.
@MikeShah
@MikeShah 2 жыл бұрын
Thank you for the kind words! The () operator being overloaded is the key here for how this is working.
@nicholashanson9508
@nicholashanson9508 2 ай бұрын
i didn't get the advantage of a functor over using the member function for comparison
@MikeShah
@MikeShah 2 ай бұрын
The functor allows us to pass 'different' 'function objects' while the program is running -- i.e. we can change how objects are compared at run-time, versus just having the member function which gives us exactly 1 comparison. We could do some things like pass a function pointer or something into our comparison operator, but it's more extensible (and provides a single responsibility) to just have a separate 'functor' for the comparison.
@Vibratebalance
@Vibratebalance Жыл бұрын
As always thanks Mike. Do you have any videos on std::function?
@MikeShah
@MikeShah Жыл бұрын
Cheers! I have one planned for sometime in the future 🙂
@Vibratebalance
@Vibratebalance Жыл бұрын
@@MikeShah Awesome! maybe in regards to a theoretical callback scenario? :)
@bimblebom
@bimblebom Жыл бұрын
Yes very clear examples but one doubt - why are you returning NewResult which is passed in - instead of m_result1 and m_result2?
@MikeShah
@MikeShah Жыл бұрын
I suppose they're the same value -- probably better to return the member as you suggest
@HousseinAlDroubi
@HousseinAlDroubi 5 ай бұрын
Hey, good man, what about you.
@theforeskinsnatcher373
@theforeskinsnatcher373 Жыл бұрын
this just seems like a needlessly complicated way of creating an object
@MikeShah
@MikeShah Жыл бұрын
Lambda's behind the scenes are just functors. They're a necessary building block for carrying state for a function call. Can actually be quite useful. As always, right tool for right job should be used.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
Cursed C++ Casts
17:41
Logan Smith
Рет қаралды 79 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 343 М.
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 858 М.
Rust Functions Are Weird (But Be Glad)
19:52
Logan Smith
Рет қаралды 157 М.
Andrew Kelley   Practical Data Oriented Design (DoD)
46:40
ChimiChanga
Рет қаралды 164 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН