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....

КОМЕНТАРІ • 39

  • @kenk4552
    @kenk4552 6 місяців тому +6

    Your modern Cpp series is a hidden gem. Will for sure get more views!!

  • @rocknroooollllll
    @rocknroooollllll 2 дні тому +1

    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.

    • @MikeShah
      @MikeShah  День тому

      Thanks! Mmm, I'll work on it 😅

  • @buddyairguy2249
    @buddyairguy2249 11 місяців тому +2

    As usual -> Great lesson!

  • @LeflxLight
    @LeflxLight 9 місяців тому +1

    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😀😀

  • @bsdooby
    @bsdooby 2 роки тому +6

    "A functor is an instance of type T whose call operator () is overloaded". This implies state.

  • @alexanderwhillas6431
    @alexanderwhillas6431 2 роки тому +6

    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 роки тому +1

      Indeed, not as strict as the category theory definition what I'm showing here, but a monad would be more related. :)

  • @michapawlik6501
    @michapawlik6501 9 місяців тому +1

    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!

  • @papertowers4216
    @papertowers4216 2 роки тому +7

    give the goblin his gold

  • @xbz24
    @xbz24 10 місяців тому +1

    Cheers

  • @kirandhegaskar9298
    @kirandhegaskar9298 10 місяців тому +1

    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

  • @peufipeufhhh2405
    @peufipeufhhh2405 3 місяці тому

    Where can we get those slides?

  • @nicholashanson9508
    @nicholashanson9508 3 місяці тому

    i didn't get the advantage of a functor over using the member function for comparison

    • @MikeShah
      @MikeShah  3 місяці тому

      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.

  • @PaulFromMalta
    @PaulFromMalta Рік тому +1

    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.

  • @smarthumanism3221
    @smarthumanism3221 2 роки тому +1

    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.

  • @mamariomiamo5596
    @mamariomiamo5596 2 роки тому +4

    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 роки тому +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 роки тому +2

      I have the same question

    • @MikeShah
      @MikeShah  2 роки тому +3

      ​@@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.

  • @sumitpatil1955
    @sumitpatil1955 Рік тому +1

    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  Рік тому +1

      Hmm, you need at the least a forward declaration for that function (e.g. something like myclass::operator()).

  • @bimblebom
    @bimblebom Рік тому +1

    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

  • @Vibratebalance
    @Vibratebalance Рік тому +1

    As always thanks Mike. Do you have any videos on std::function?

    • @MikeShah
      @MikeShah  Рік тому +1

      Cheers! I have one planned for sometime in the future 🙂

    • @Vibratebalance
      @Vibratebalance Рік тому +1

      @@MikeShah Awesome! maybe in regards to a theoretical callback scenario? :)

  • @HousseinAlDroubi
    @HousseinAlDroubi 5 місяців тому

    Hey, good man, what about you.

  • @theforeskinsnatcher373
    @theforeskinsnatcher373 Рік тому +1

    this just seems like a needlessly complicated way of creating an object

    • @MikeShah
      @MikeShah  Рік тому +1

      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.