Functors() - Function objects - functions with state | Modern Cpp Series Ep. 99
Вставка
- Опубліковано 5 лют 2025
- ►Full C++ Series Playlist: • The C++ Programming La...
►Find full courses on: courses.mshah.io/
►Join as Member to Support the channel: / @mikeshah
►Lesson Description: In this lesson we learn about a concept that probably sounds scarier than it really should be -- functors. These are 'function objects' in which we can 'hold state' (because they are a class), but use exactly like a function. Where functors() really shine is by allowing us to compose or partially compose results, and we're soon going to find out that lambda's (unnamed functions) are essentially functors behind the scenes, but both lambda's and functors are incredibly useful tools for using with the STL.
►UA-cam Channel: / mikeshah
►Please like and subscribe to help the channel!
►Join our free community: courses.mshah....
Your modern Cpp series is a hidden gem. Will for sure get more views!!
Cheers!
Great ep, Mike. Thank you. A little feedback: you say the word 'here' like 150 times , maybe more (I didn't count them!) Once you notice it, you can't unhear it! But I do like your videos.
Thanks! Mmm, I'll work on it 😅
As usual -> Great lesson!
Cheers!
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😀😀
Cheers!
"A functor is an instance of type T whose call operator () is overloaded". This implies state.
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.
Indeed, not as strict as the category theory definition what I'm showing here, but a monad would be more related. :)
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
Cheers, happy to hear that!
give the goblin his gold
😁
Cheers
🥂
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
Where can we get those slides?
i didn't get the advantage of a functor over using the member function for comparison
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.
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?
For conparator, could use general function if no state needed, can also use things like std::less or lambda.
Than you for the excellent teaching. If I may, I'd like to know how this works internally.
Thank you for the kind words! The () operator being overloaded is the key here for how this is working.
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?
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 😀
I have the same question
@@Southpaw101 Hi team, that's correct. We avoid a copy with initializer list (ua-cam.com/video/vjAJ4ESX1Jc/v-deo.html) and then use something like: m_health(std::move(health)) and avoid making extra copies.
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?
Hmm, you need at the least a forward declaration for that function (e.g. something like myclass::operator()).
Yes very clear examples but one doubt - why are you returning NewResult which is passed in - instead of m_result1 and m_result2?
I suppose they're the same value -- probably better to return the member as you suggest
As always thanks Mike. Do you have any videos on std::function?
Cheers! I have one planned for sometime in the future 🙂
@@MikeShah Awesome! maybe in regards to a theoretical callback scenario? :)
Hey, good man, what about you.
this just seems like a needlessly complicated way of creating an object
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.